pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <bdo id='oJMbW'></bdo><ul id='oJMbW'></ul>
    <i id='oJMbW'><tr id='oJMbW'><dt id='oJMbW'><q id='oJMbW'><span id='oJMbW'><b id='oJMbW'><form id='oJMbW'><ins id='oJMbW'></ins><ul id='oJMbW'></ul><sub id='oJMbW'></sub></form><legend id='oJMbW'></legend><bdo id='oJMbW'><pre id='oJMbW'><center id='oJMbW'></center></pre></bdo></b><th id='oJMbW'></th></span></q></dt></tr></i><div class="5rtv7lj" id='oJMbW'><tfoot id='oJMbW'></tfoot><dl id='oJMbW'><fieldset id='oJMbW'></fieldset></dl></div>

  1. <legend id='oJMbW'><style id='oJMbW'><dir id='oJMbW'><q id='oJMbW'></q></dir></style></legend>

  2. <tfoot id='oJMbW'></tfoot>

    1. <small id='oJMbW'></small><noframes id='oJMbW'>

      站點 coderbyte 上的“gets(stdin)"是怎么回事?

      What is going on with #39;gets(stdin)#39; on the site coderbyte?(站點 coderbyte 上的“gets(stdin)是怎么回事?)

      <legend id='2hpXn'><style id='2hpXn'><dir id='2hpXn'><q id='2hpXn'></q></dir></style></legend>

        <small id='2hpXn'></small><noframes id='2hpXn'>

        <i id='2hpXn'><tr id='2hpXn'><dt id='2hpXn'><q id='2hpXn'><span id='2hpXn'><b id='2hpXn'><form id='2hpXn'><ins id='2hpXn'></ins><ul id='2hpXn'></ul><sub id='2hpXn'></sub></form><legend id='2hpXn'></legend><bdo id='2hpXn'><pre id='2hpXn'><center id='2hpXn'></center></pre></bdo></b><th id='2hpXn'></th></span></q></dt></tr></i><div class="ph7jdj5" id='2hpXn'><tfoot id='2hpXn'></tfoot><dl id='2hpXn'><fieldset id='2hpXn'></fieldset></dl></div>

      1. <tfoot id='2hpXn'></tfoot>
            <tbody id='2hpXn'></tbody>
                <bdo id='2hpXn'></bdo><ul id='2hpXn'></ul>
                本文介紹了站點 coderbyte 上的“gets(stdin)"是怎么回事?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                Coderbyte 是一個在線編程挑戰網站(我在 2 分鐘前發現它).

                您遇到的第一個 C++ 挑戰有一個 C++ 框架你需要修改:

                <塊引用>

                #include #include <字符串>使用命名空間標準;int FirstFactorial(int num) {//代碼在這里返回編號;}int main() {//保持這個函數調用在這里cout<<FirstFactorial(gets(stdin));返回0;}

                如果您對 C++ 不太熟悉,首先* 會出現在您的眼前:

                int FirstFactorial(int num);cout<<FirstFactorial(gets(stdin));

                所以,好的,代碼調用了 gets,它自 C++11 起已被棄用,自 C++14 起被刪除,這本身就很糟糕.

                但后來我意識到:getschar*(char*) 類型.所以它不應該接受 FILE* 參數并且結果不應該用于代替 int 參數,但是......不僅它編譯時沒有任何警告或錯誤,但它運行并實際將正確的輸入值傳遞給 FirstFactorial.

                在此特定站點之外,代碼無法編譯(如預期),那么這里發生了什么?

                <小時>

                *實際上第一個是 using namespace std 但這與我在這里的問題無關.

                解決方案

                我很好奇.所以,是時候戴上調查眼鏡了,因為我無法訪問編譯器或編譯標志,所以我需要發揮創造力.此外,因為這段代碼沒有任何意義,所以對每個假設提出質疑并不是一個壞主意.

                首先讓我們檢查gets的實際類型.我有一個小技巧:

                模板結構名稱;int main() {名稱n;//保持這個函數調用在這里cout<<FirstFactorial(gets(stdin));返回0;}

                這看起來……很正常:

                <塊引用>

                /tmp/613814454/Main.cpp:16:19: 警告:'gets' 已被棄用 [-Wdeprecated-declarations]名稱n;^/usr/include/stdio.h:638:37: 注意:'gets' 已在此處明確標記為已棄用extern char *gets (char *__s) __wur __attribute_deprecated__;^/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51:注意:從宏__attribute_deprecated__"擴展# 定義 __attribute_deprecated__ __attribute__ ((__deprecated__))^/tmp/613814454/Main.cpp:16:26: 錯誤:未定義模板的隱式實例化 'Name'名稱n;^/tmp/613814454/Main.cpp:12:25:注意:模板在此處聲明模板<類>結構名稱;^生成了 1 個警告和 1 個錯誤.

                gets 被標記為已棄用并具有簽名 char *(char *).但是,FirstFactorial(gets(stdin)); 是如何編譯的?

                讓我們試試別的:

                int main() {名稱n;//保持這個函數調用在這里cout<<FirstFactorial(gets(stdin));返回0;}

                這給了我們:

                <塊引用>

                /tmp/286775780/Main.cpp:15:21: 錯誤:未定義模板 'Name' 的隱式實例化名稱n;^

                我們終于得到了一些東西:decltype(8).所以整個 gets(stdin) 被文本替換為輸入 (8).

                事情變得更奇怪了.編譯器錯誤繼續:

                <塊引用>

                /tmp/596773533/Main.cpp:18:26: 錯誤:沒有匹配的函數調用gets"cout<<FirstFactorial(gets(stdin));^~~~/usr/include/stdio.h:638:14:注意:候選函數不可行:第一個參數沒有從struct _IO_FILE *"到char *"的已知轉換extern char *gets (char *__s) __wur __attribute_deprecated__;

                所以現在我們得到了 cout << 的預期錯誤.FirstFactorial(gets(stdin));

                我檢查了一個宏,因為 #undef gets 似乎什么都不做,所以它看起來不是一個宏.

                但是

                std::integral_constantn;

                它編譯.

                但是

                std::integral_constantn;//行std::integral_constantn2;//錯誤 wtf??

                n2 行沒有出現預期的錯誤.

                再說一次,幾乎對 main 的任何修改都會使 cout <<FirstFactorial(gets(stdin)); 吐出預期的錯誤.

                此外,stdin 實際上似乎是空的.

                所以我只能得出結論并推測他們有一個小程序可以解析源代碼并嘗試(很差)在將 gets(stdin) 替換為測試用例輸入值之前實際將其輸入編譯器.如果有人有更好的理論或實際知道他們在做什么,請分享!

                這顯然是一種非常糟糕的做法.在研究這個時,我發現這里至少有一個問題(example) 關于這一點,因為人們不知道有一個網站在那里做這件事,他們的答案是不要使用 gets 而是使用 ...".這確實是一個很好的建議,但只會讓 OP 更加困惑,因為任何從 stdin 進行有效讀取的嘗試都將在此站點上失敗.


                TLDR

                gets(stdin) 是無效的 C++.這是這個特定網站使用的噱頭(我不知道是什么原因).如果你想繼續在網站上提交(我既不認可也不不認可它)你必須使用這個結構,否則就沒有意義,但要注意它是脆弱的.幾乎對 main 的任何修改都會拋出錯誤.在本站之外使用正常的輸入閱讀方法.

                Coderbyte is an online coding challenge site (I found it just 2 minutes ago).

                The first C++ challenge you are greeted with has a C++ skeleton you need to modify:

                #include <iostream>
                #include <string>
                using namespace std;
                
                int FirstFactorial(int num) {
                
                  // Code goes here
                  return num;
                
                }
                
                int main() {
                
                  // Keep this function call here
                  cout << FirstFactorial(gets(stdin));
                  return 0;
                
                }
                

                If you are little familiar with C++ the first thing* that pops in your eyes is:

                int FirstFactorial(int num);
                cout << FirstFactorial(gets(stdin));
                

                So, ok, the code calls gets which is deprecated since C++11 and removed since C++14 which is bad in itself.

                But then I realize: gets is of type char*(char*). So it shouldn't accept a FILE* parameter and the result shouldn't be usable in the place of an int parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial.

                Outside of this particular site, the code doesn't compile (as expected), so what is going on here?


                *Actually the first one is using namespace std but that is irrelevant to my issue here.

                解決方案

                I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.

                First let's check the actual type of gets. I have a little trick for that:

                template <class> struct Name;
                
                int main() { 
                    
                    Name<decltype(gets)> n;
                  
                  // keep this function call here
                  cout << FirstFactorial(gets(stdin));
                  return 0;
                    
                }
                

                And that looks ... normal:

                /tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
                    Name<decltype(gets)> n;
                                  ^
                /usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
                extern char *gets (char *__s) __wur __attribute_deprecated__;
                                                    ^
                /usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
                # define __attribute_deprecated__ __attribute__ ((__deprecated__))
                                                                  ^
                /tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
                    Name<decltype(gets)> n;
                                         ^
                /tmp/613814454/Main.cpp:12:25: note: template is declared here
                template <class> struct Name;
                                        ^
                1 warning and 1 error generated.
                

                gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?

                Let's try something else:

                int main() { 
                  Name<decltype(gets(stdin))> n;
                  
                  // keep this function call here
                  cout << FirstFactorial(gets(stdin));
                  return 0;
                    
                } 
                

                Which gives us:

                /tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
                  Name<decltype(8)> n;
                                    ^
                

                Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).

                And the things get weirder. The compiler error continues:

                /tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
                  cout << FirstFactorial(gets(stdin));
                                         ^~~~
                /usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
                extern char *gets (char *__s) __wur __attribute_deprecated__;
                

                So now we get the expected error for cout << FirstFactorial(gets(stdin));

                I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.

                But

                std::integral_constant<int, gets(stdin)> n;
                

                It compiles.

                But

                std::integral_constant<int, gets(stdin)> n;    // OK
                std::integral_constant<int, gets(stdin)> n2;   // ERROR                                          wtf??
                

                Doesn't with the expected error at the n2 line.

                And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.

                Moreover the stdin actually seems to be empty.

                So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!

                This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.


                TLDR

                gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.

                這篇關于站點 coderbyte 上的“gets(stdin)"是怎么回事?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡單方法?)
                Reading from .txt file into two dimensional array in c++(從 .txt 文件讀取到 C++ 中的二維數組)
                How to simulate a key press in C++(如何在 C++ 中模擬按鍵按下)
                Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(為什么在 cin.ignore() 之后沒有 getline(cin, var) 讀取字符串的第一個字符?)
                What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)
                  <bdo id='0YFe3'></bdo><ul id='0YFe3'></ul>
                  <i id='0YFe3'><tr id='0YFe3'><dt id='0YFe3'><q id='0YFe3'><span id='0YFe3'><b id='0YFe3'><form id='0YFe3'><ins id='0YFe3'></ins><ul id='0YFe3'></ul><sub id='0YFe3'></sub></form><legend id='0YFe3'></legend><bdo id='0YFe3'><pre id='0YFe3'><center id='0YFe3'></center></pre></bdo></b><th id='0YFe3'></th></span></q></dt></tr></i><div class="llbpnt7" id='0YFe3'><tfoot id='0YFe3'></tfoot><dl id='0YFe3'><fieldset id='0YFe3'></fieldset></dl></div>

                      <tbody id='0YFe3'></tbody>
                    <legend id='0YFe3'><style id='0YFe3'><dir id='0YFe3'><q id='0YFe3'></q></dir></style></legend>

                        <small id='0YFe3'></small><noframes id='0YFe3'>

                      1. <tfoot id='0YFe3'></tfoot>

                          主站蜘蛛池模板: 电池高低温试验箱-气态冲击箱-双层电池防爆箱|简户百科 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 楼承板-开闭口楼承板-无锡海逵楼承板| 广州食堂承包_广州团餐配送_广州堂食餐饮服务公司 - 旺记餐饮 | 荣事达手推洗地机_洗地机厂家_驾驶式扫地机_工业清洁设备 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 液压油缸-液压缸厂家价格,液压站系统-山东国立液压制造有限公司 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 耙式干燥机_真空耙式干燥机厂家-无锡鹏茂化工装备有限公司 | 高扬程排污泵_隔膜泵_磁力泵_节能自吸离心水泵厂家-【上海博洋】 | 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | OpenI 启智 新一代人工智能开源开放平台| 云杂志网-学术期刊-首页| 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 水厂污泥地磅|污泥处理地磅厂家|地磅无人值守称重系统升级改造|地磅自动称重系统维修-河南成辉电子科技有限公司 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | 天津电机维修|水泵维修-天津晟佳机电设备有限公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 氢氧化钙设备_厂家-淄博工贸有限公司 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 亿诺千企网-企业核心产品贸易| 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 99文库_实习生实用的范文资料文库站 | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | 医用空气消毒机-医用管路消毒机-工作服消毒柜-成都三康王 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 | 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 警用|治安|保安|不锈钢岗亭-售货亭价格-垃圾分类亭-移动厕所厂家-苏州灿宇建材 | 天津散热器_天津暖气片_天津安尼威尔散热器制造有限公司 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 安平县鑫川金属丝网制品有限公司,防风抑尘网,单峰防风抑尘,不锈钢防风抑尘网,铝板防风抑尘网,镀铝锌防风抑尘网 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 北京网站建设|北京网站开发|北京网站设计|高端做网站公司 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 |