å®éã«ã¯ãç¹å®ã®ãã©ãããã©ãŒã ã®ç¹å®ã®ã¿ã¹ã¯ã解決ããéçºè ã¯ããC ++æšæºã«æ£ç¢ºã«å¯Ÿå¿ããŠããŸããïŒ ã³ã³ãã€ã©ã®æ¡åŒµæ©èœã§ããå Žåã¯ã©ããªããŸãããã 圌ãã¯ã³ãŒããæžãããã«ããéå§ããã³ã³ãã€ã©ãèªã£ãå Žæã修埩ããŸãã
ãã®çµæãç¹å®ã®ã³ã³ãã€ã©ãŒïŒããã³ãã®ç¹å®ã®ããŒãžã§ã³ïŒïŒããã³ã¿ãŒã²ããOSã«ããçšåºŠå¯Ÿå¿ããã¢ããªã±ãŒã·ã§ã³ãåŸãããŸãã ããã«ãC ++æšæºã©ã€ãã©ãªãäžè¶³ããŠãããããç¹å®ã®ã·ã¹ãã APIã䜿çšããã«èšè¿°ã§ããªããã®ããããŸãã
Tensorã§ç§ãã¡ãšäžç·ã«ããŸããã MS Visual Studio 2010ã§äœæããŸãããåœç€Ÿã®è£œåã¯32ãããWindowsã¢ããªã±ãŒã·ã§ã³ã§ããã ãããŠãã¡ãããã³ãŒãã¯ãã€ã¯ããœããã®ãã¯ãããžãŒãšã®ããããçš®é¡ã®çµã³ã€ãã«æºã¡ãŠããŸããã æ°ããèŠéãæ¢ããšãã ãšå€æããããVLSIã«Linuxãä»ã®OSã§åäœããããã«æãããšãã§ãããå¥ã®ããŒããŠã§ã¢ïŒPOWERïŒã«åãæ¿ããããšãè©Šã¿ããšãã§ããã
ãã®äžé£ã®èšäºã§ã¯ã補åãå®éã®ã¯ãã¹ãã©ãããã©ãŒã ã¢ããªã±ãŒã·ã§ã³ã«ããæ¹æ³ã説æããŸãã LinuxãMacOSãããã«ã¯iOSãAndroidã§ã©ã®ããã«æ©èœããããã ããŸããŸãªããŒããŠã§ã¢ã¢ãŒããã¯ãã£ïŒx86-64ãPOWERãARMãªã©ïŒã§ã¢ããªã±ãŒã·ã§ã³ãèµ·åããæ¹æ³ã ããã°ãšã³ãã£ã¢ã³ã®ãã·ã³ã§ã®äœæ¥ãåŠã¶æ¹æ³ã
ãã¹ãŠã®è£œåã®åºç€ã¯ãèŠæš¡ãQtã«å¹æµããç¬èªã®VLSIãã©ãããã©ãŒã ãã¬ãŒã ã¯ãŒã¯ïŒä»¥äžããã©ãããã©ãŒã ããšåŒã³ãŸãïŒã§ãã ãã©ãããã©ãŒã ã«ã¯ãéçºè ãå¿ èŠãšããã»ãŒãã¹ãŠã®ãã®ããããŸããæ°å€ãæåå圢åŒã«ãã°ããå€æããããã®åçŽãªé¢æ°ããã匷åãªãã©ãŒã«ããã¬ã©ã³ãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãŸã§ã
ãã©ãããã©ãŒã ã«åºã¥ããŠãåœç€Ÿã®éçºè ã¯ãããããçš®é¡ã®ããžãã¹äžã®åé¡ã解決ãã補åïŒã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³ãå«ãïŒãå®è£ ããŸãã ã³ãŒãã解æŸãïŒä»¥äžãã³ãŒãããé©çšæžã¿ããšåŒã³ãŸãïŒãã¿ãŒã²ãããœãããŠã§ã¢ããã³ããŒããŠã§ã¢ãã©ãããã©ãŒã ãšã®ããããçš®é¡ã®çµã³ä»ããã解æŸãããã¬ãŒã ã¯ãŒã¯ã®å¥¥æ·±ãã«ãããã¹ãŠã®ä»æ§ãé ããŸããã
VLSIãã©ãããã©ãŒã ã¯C ++ã§èšè¿°ãããŠããŸãããããã¯ã¢ããªã±ãŒã·ã§ã³ããã°ã©ããŒãèšèªãéžæããã®ãå¶éãããã®ã§ã¯ãªããC ++ JavaScriptãPythonãããã³SQLã䜿çšã§ããŸãã
åœç€Ÿã¯ç©æ¥µçã«è£œåãéçºããŠããã®ã§ãããã«ã¹ããŒãã§é»è»ãä¿®çãããå¿ èŠããããŸãã:)
ä»ã®éçºè ãç§ãã¡ã®æŽ»åã«æ©ãŸããããMSVCã§Windowsæ©èœãå¿«é©ã«éçºãç¶ããããšãã§ããããã«äœæ¥ããå¿ èŠããããŸããã ãã®èŠä»¶ã¯å€ãã®æè¡çãœãªã¥ãŒã·ã§ã³ã«å€§ããªåœ±é¿ãäžããäœæ¥ãéåžžã«è€éã«ããŸããã
èªè ãäœåã®èŠæš¡ãç解ããããã«ãããã€ãã®æ°åãæããŸãã
- ç§ãã¡ã®ãã¬ãŒã ã¯ãŒã¯ã®ã³ãŒãéã¯ã200äžè¡ã§ã
- ãé©çšããããã³ãŒãïŒç¹å®ã®ããžãã¹äžã®åé¡ã解決ããVLSIãã©ãããã©ãŒã ã«åºã¥ãã³ãŒãïŒã®éãè©äŸ¡ããããšã¯å°é£ã§ããããã©ãããã©ãŒã ã®éã®æ°åã§ã
- 10ã®éçºã»ã³ã¿ãŒã«ãã1000人以äžã®ããã°ã©ããŒ
éå±ãªãšã³ããªãŒã¯çµãããŸããã ããã§ã¯åé¡ã«è¿ã¥ããã©ã®ãããªåé¡ã«ééããããèããŠã¿ãŸãããã
ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã APIã®äœ¿çš
åè¿°ã®ããã«ãC ++æšæºã©ã€ãã©ãªã¯éåžžã«è²§åŒ±ã§ãããå¿ èŠãªæ©èœã®å€ããè³ãæã«å«ãŸããŠããŸããã ããšãã°ãC ++ 11ã§ã¯ããããã¯ãŒã¯ãæäœããããã®æ©èœã¯ãããŸãã...ã€ãŸããæãåçŽãªHTTPãªã¯ãšã¹ããäœæãããšããã«ã匷å¶çã«...éãã©ãããã©ãŒã ã³ãŒããèšè¿°ããªããã°ãªããŸããã
ææ°ããŒãžã§ã³ã®ã³ã³ãã€ã©ã䜿çšããŠããªãå Žåãç¶æ³ã¯ããã«æªåããŸããMSVS2010ã§ã¯ãC ++ 11ã®å«ãªãµããŒãããããã³ã¢èšèªãšæšæºã©ã€ãã©ãªã®é©æ°ã®å€§éšåããããŸãã
ãããã幞ããªããšã«ããã®ãããªåé¡ã¯éåžžã«ç°¡åã«è§£æ±ºã§ããŸãã ããã€ãã®æ¹æ³ããããŸãã
- ã¿ãŒã²ããã·ã¹ãã ã®APIåŒã³åºãã«åºã¥ããã©ãããã©ãŒã åºæã®å®è£ ãããã€ã䜿çšããŠãç¬èªã®ã¯ã©ã¹ãäœæããŠããŸãã ã¢ã»ã³ããªäžã«ãifdefããªããã»ããµãã£ã¬ã¯ãã£ãã¯é©åãªå®è£ ãéžæããŸãã
- ç§ãã¡ã¯ã¯ãã¹ãã©ãããã©ãŒã ã©ã€ãã©ãªã䜿çšããŠããŸã-ç§ãã¡ã®ã¿ã¹ã¯ãå€§å¹ ã«ç°¡çŽ åããå€ãã®æ¢è£œã®ã¯ãã¹ãã©ãããã©ãŒã ã©ã€ãã©ãªããããŸãïŒããã§ãããã©ãããã©ãŒã åºæã®å®è£ ã䜿çšããŠããŸãïŒã ããšãã°ãHTTPã¯ã©ã€ã¢ã³ããå®è£ ããããã«ãcURLã䜿çšããŸããã
ã³ã³ãã€ã©ãŒå®è£ ã®æ©èœ
åããã°ã©ã ã«ã¯ãšã©ãŒããããŸãã ãããŠãã³ã³ãã€ã©ãäŸå€ã§ã¯ãããŸããã ãããã£ãŠã100ïŒ æšæºã«æºæ ããã³ãŒãã§ãããããçš®ã®ã³ã³ãã€ã©ã§ã³ã³ãã€ã«ãããªãå ŽåããããŸãã
ãŸããã»ãšãã©ãã¹ãŠã®ã³ã³ãã€ã©éçºè ã¯ãæšæºã§ã¯æäŸãããŠããªãæ©èœãèªåã®é è³ã«è¿œå ããããšã矩åãšèããŠãããããã«ãã£ãŠããã°ã©ãã«èããããªãã³ãŒããæžãããä¿ããŠããŸãã
æåŸã«äœãåŸãããŸããïŒ æšæºã«åŸã£ãŠæ確ã«èšè¿°ãããã³ãŒãã¯ãäžéšã®ã³ã³ãã€ã©ã§ã¯ã³ã³ãã€ã«ã§ããªãå ŽåããããŸãã ããã³ã³ãã€ã©ã§ã³ã³ãã€ã«ããã³å®è¡ãããã³ãŒãã¯ãã³ã³ãã€ã«ãããªãããå¥ã®ã³ã³ãã€ã©ã§æ£ããåäœããªãå¯èœæ§ããããŸã...
ãã®ã¯ã©ã¹ã®å€ãã®åé¡ããªã¹ãã§ããŸãã ãããã®1ã€ã次ã«ç€ºããŸãã
throw std::exception( "- " ); // MSVC++,
ãã®ã³ãŒãã¯ãè¿œå ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãå®çŸ©ãããŠãããããMSVC ++ã§ã³ã³ãã€ã«ãããŸãã
exception( const char* msg ) noexcept;
æ®å¿µãªããããã®ãããªåé¡ã解決ããäžè¬çãªããªãã¯ã¯ãããŸããã ãããã®å Žåãäœæ¥ã§äœ¿çšãããããŒã«ã®åŠç¿ã§åŸãçµéšãšãC ++æšæºãã«ãã®ååãªç¥èã®ã¿ãåŸãããŸãã
åŸç¶ã®èšäºã§ã¯ããã®åé¡ã«æ»ããæãäžè¬çãªåé¡ã詳现ã«èª¬æãããããã解決ããæ¹æ³ãææ¡ããŸãã
æªå®çŸ©ã®åäœ
C ++æšæºã«ã¯ããæªå®çŸ©ã®åäœããšããèå³æ·±ãçšèªããããŸãã ãŠã£ãããã£ã¢ããã®åœŒã®å®çŸ©ã¯æ¬¡ã®ãšããã§ãã
æªå®çŸ©ã®åäœïŒè±èªã®æªå®çŸ©ã®åäœãå€ãã®ãœãŒã¹ã§ã®äºæž¬äžå¯èœãªåäœ[1] [2]ïŒã¯ãäžéšã®ããã°ã©ãã³ã°èšèªïŒCã§æãé¡èïŒããœãããŠã§ã¢ã©ã€ãã©ãªãããã³ã³ã³ãã€ã©ã®å®è£ ïŒã©ã€ãã©ãªïŒãããïŒããã³ã¡ã¢ãªã¹ããŒã¿ã¹ãããªã¬ãŒãããå²ã蟌ã¿ãªã©ã®ã©ã³ãã ãªèŠå ã èšãæãããšãä»æ§ã§ã¯ãå¯èœãªç¶æ³ã§ã®èšèªïŒã©ã€ãã©ãªããã€ã¯ããµãŒãããïŒã®åäœã¯æ±ºå®ãããŸãããããæ¡ä»¶Aã§ã¯ãæäœBã®çµæã¯å®çŸ©ãããŠããŸããããšè¿°ã¹ãŠããŸãã ããã°ã©ã ã§ãã®ãããªç¶æ³ãèš±å¯ããããšã¯ééããšèŠãªãããŸãã ããã°ã©ã ãäžéšã®ã³ã³ãã€ã©ã§æ£åžžã«å®è¡ãããŠããã¯ãã¹ãã©ãããã©ãŒã ã§ã¯ãªããå¥ã®ãã·ã³ãç°ãªãOSããŸãã¯ç°ãªãã³ã³ãã€ã©èšå®ã§å€±æããå¯èœæ§ããããŸãã
ããã°ã©ã ã§æªå®çŸ©ã®åäœãèš±å¯ããå Žåãããã¯ã¯ã©ãã·ã¥ããããã³ã³ãœãŒã«ã«ãšã©ãŒãæãããããããšãæå³ãããã®ã§ã¯ãããŸããã ãã®ãããªããã°ã©ã ã¯æåŸ ã©ããã«æ©èœããå¯èœæ§ããããŸãã ãã ããã³ã³ãã€ã©ã®èšå®ãå€æŽããããå¥ã®ã³ã³ãã€ã©ãå¥ã®ããŒãžã§ã³ã«åãæ¿ããããã³ãŒããå€æŽãããšãããã°ã©ã ã®åäœãå€ããããã¹ãŠãç Žæããå¯èœæ§ããããŸãã
ç¹å®ã®ã³ã³ãã€ã©ã§æªå®çŸ©ã®åäœãçºçããå€ãã®ç¶æ³ã§ã¯ãåãåäœãå®å®ããŠçæãããæ éã«ãã¹ããããã¢ããªã±ãŒã·ã§ã³ã¯ã¹ã€ã¹æèšã®ããã«æ©èœããŸãã ããããç°å¢ãå€æŽãããšããã«ïŒããšãã°ãå¥ã®ã³ã³ãã€ã©ãŒã«ãã£ãŠã³ã³ãã€ã«ãããããã°ã©ã ãå®è¡ããããšãããšïŒããããã®ãã°ã¯å®£èšãéå§ããããã°ã©ã ãå®å šã«ç Žå£ããŸãã
æªå®çŸ©ã®åäœã®å žåçãªäŸã¯ãã¹ã¿ãã¯äžã®é åã®å¢çãè¶ ããããšã§ãã 以äžã¯ããã®åé¡ãæ±ããã¢ããªã±ãŒã·ã§ã³ã®1ã€ã®ç°¡ç¥åãããã³ãŒãã¹ããããã§ãã ãã®ãã°ã¯ãWindowsã§æ°å¹Žéã¯çŸãããLinuxãžã®ç§»æ€åŸã«ã®ã¿ãçºç ²ãããŸããã
std::string SomeFunction() { char hex[9]; // some code hex[9] = 0; // return hex; }
ã©ããããMSVSã¯ã¹ã¿ãã¯äžã®ãââããã¡ãŒã調æŽãããã®åŸã«æ°ãã€ããè¿œå ããŠããããã誰ãã®ã¡ã¢ãªãäžæžããããšã空ã®æªäœ¿çšã®å Žæã«ãªã£ãŠããŸããŸããã ãããŠãGCCã§ã¯ãåé¡ã¯èå³æ·±ãæ¹æ³ã§çŸãå§ããŸãã-ããã°ã©ã ã¯å¥ã®é¢æ°ã§ãã®ã³ãŒãããé ãé¢ããŸããïŒæããã«ãGCCã¯ãã®é¢æ°ãã€ã³ã©ã€ã³åããå¥ã®é¢æ°ã®ããŒã«ã«å€æ°ãæžãæãå§ããŸããïŒã
UBã«ã¯ããããšã¬ã¬ã³ãã§ãããã«ããç¶æ³ããããŸãã ããšãã°ãstd :: sortïŒã䜿çšãããšãéåžžã«èå³æ·±ãã¬ãŒããå®è¡ã§ããŸãã
std::vector< std::string > v = some_func(); std::sort( v.begin(), v.end(), []( const std::string& s1, const std::string& s2 ) { if( s1.empty() ) return true; return s1 < s2; } );
ã©ãã«UBãããã®ã§ããããïŒ ãããŠãåé¡ã¯ãæªããã³ã³ãã¬ãŒã¿ã«ãããŸãã
s1ãs2ã®åã«é 眮ããå¿ èŠãããå Žåãã³ã³ãã¬ãŒã¿ã¯trueãè¿ãå¿ èŠããããŸãã 2ã€ã®ç©ºã®è¡ãå ¥åãããå Žåã«ã³ã³ãã¬ãŒã¿ãŒãçæãããã®ãæ€èšããŸãã
s1 = "";
s2 = "";
cmpïŒs1ãs2ïŒ== true => s1ã¯s2ã®åã«ãªããã°ãªããŸãã
cmpïŒs2ãs1ïŒ== true => s2ã¯s1ã®åã«ãªããã°ãªããŸãã
ãããã£ãŠãã³ã³ãã¬ãŒã¿ãããèªäœã«ççŸããå Žåãã€ãŸãå³å¯ãªåŒ±ãé åºä»ããæå®ããªãå ŽåããããŸãïŒ en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderingsãžã®ãªã³ã¯ïŒã ãããã£ãŠãåŒæ°ã®std :: sortã®èŠä»¶ã«éåããæªå®çŸ©ã®åäœãçºçããŸããã
ãããŠãããã¯çºæãããäŸã§ã¯ãããŸããã Linuxã«åãæ¿ãããšãã«ãã®åé¡ãçºèŠããŸããã åæ§ã®ãšã©ãŒã®ã³ã³ãã¬ãŒã¿ã¯ãWindowsã§é·å¹Žæ©èœãã... LinuxïŒi686ïŒã§SIGSEGVã䜿çšããŠã¢ããªã±ãŒã·ã§ã³ãã¯ã©ãã·ã¥ããå§ããŸããã èå³æ·±ãããšã«ããã°ã¯ç°ãªãLinuxãã£ã¹ããªãã¥ãŒã·ã§ã³ïŒç°ãªãGCCãæèŒïŒã§ãç°ãªãåäœãããŸããã¢ããªã±ãŒã·ã§ã³ãã¯ã©ãã·ã¥ããããããªãŒãºããããåçŽã«ãœãŒãããããããŸãã
å€ãã®å Žåãæªå®çŸ©ã®åäœã䌎ãç¶æ³ã¯ãéçã¢ãã©ã€ã¶ãŒïŒã³ã³ãã€ã©ãŒã«çµã¿èŸŒãŸããŠãããã®ãå«ãïŒã«ãã£ãŠãã£ããã§ããŸãã ãããã£ãŠããã«ãèšå®ã§ã¯ãåžžã«èŠåã®æ倧ã¬ãã«ãèšå®ããå¿ èŠããããŸãã ãŸãããæªäœ¿çšã®å€æ°ãã¿ã€ãã®èŠåã®çŸ€éã§æçšãªèŠåã倱ããªãããã«ããã«ã¯ãã³ãŒããäžåºŠã¯ãªãŒã³ã¢ãããããèŠåããšã©ãŒãšããŠæ±ããã¢ã»ã³ããªãªãã·ã§ã³ãæå¹ã«ããŠãæ°ä»ããªãæ°ããèŠåã衚瀺ãããªãããã«ããŸãã
ããŒã¿ã¢ãã«
C ++æšæºã¯ãã³ã³ãã¥ãŒã¿ãŒã®ã¡ã¢ãªå ã®ããŒã¿åã®è¡šçŸã«é¢ããå³å¯ãªä¿èšŒãæäŸããŠããŸããã ããã€ãã®é¢ä¿ã®ã¿ãå®çŸ©ãïŒããšãã°ãsizeofïŒcharïŒ<= sizeofïŒshortïŒ<= sizeofïŒintïŒ<= sizeofïŒlongïŒ<= sizeofïŒlong longïŒïŒãåã®ç¹æ§ã決å®ããæ¹æ³ãæäŸããŸãã
ç°ãªãã·ã¹ãã ã§ã¯ãåã®è¡šçŸæ¹æ³ã¯å€§ããç°ãªãå ŽåããããŸãã åºæ¬åã®æ¬¡å ã¯ãããŒã¿ã¢ãã«ã«ãã£ãŠå®çŸ©ãããŸãã ããŒã¿ã¢ãã«ã¯ãéçºç°å¢å ã§æ¡çšãããŠããåã®ãã£ã¡ã³ã·ã§ã³ã®æ¯çãšããŠç解ãããå¿ èŠããããŸãã 以äžã®è¡šã¯ãäžè¬çãªããŒã¿ã¢ãã«ãšãäž»èŠãªC ++åã®å¯Ÿå¿ãã次å ã瀺ããŠããŸãã
ã»ãšãã©ã®å Žåãããã°ã©ãã¯ãããŒã¿åãéžæãããšãã«ããã®ãµã€ãºã«ã€ããŠä¿èšŒããå¿ èŠããããŸãã ããããå®éã«ã¯ãéçºè ã¯å€ãã®å Žåãäœæ¥ããã·ã¹ãã å ã®åºæ¬åã®ãµã€ãºã«åçŽã«çžãããŠããŸãã ç¹°ãè¿ããŸãããå¥ã®ãœãããŠã§ã¢ãŸãã¯ããŒããŠã§ã¢ãã©ãããã©ãŒã ã«åãæ¿ãããšãé©ããçããŸããäžéšã®ã³ãŒãã¯ã³ã³ãã€ã«ãåæ¢ããäžéšã¯ç°ãªãåäœãéå§ããããå®å šã«åäœãåæ¢ããŸãã
ããšãã°ã次ã®ããã·ã¥é¢æ°ã¯ãç°ãªããã©ãããã©ãŒã ã§èµ·åãããšãåãããŒã¿ã«å¯ŸããŠç°ãªãçµæãçæããŸãã
unsigned long some_hash( const unsigned char* buf, size_t size ) { unsigned long res = 0; for( size_t i = 0; i < size; ++i ) res = res * buf[i] + buf[i] + i; return res; }
ãããã®åé¡ã®ã»ãšãã©ã¯ããµã€ãºãä¿èšŒãããåã䜿çšããŠè§£æ±ºã§ããŸãã
std::int8_t, std::int16_t . . std::uint32_t some_hash( const unsigned char* buf, size_t size ) { std::uint32_t res = 0; for( size_t i = 0; i < size; ++i ) res = res * buf[i] + buf[i] + i; return res; }
ãã£ãŒãµã€ã³
charããµã€ã³ãªã®ãçåã«æã£ãŠããéçºè ã¯ããŸãããªããšæããŸãã ãããŠããã®ãããªè³ªåãçããå Žåãã»ãšãã©ã®äººã¯ãæ°ã«å ¥ãã®éçºç°å¢ãéããå°ããªãã¹ãããã°ã©ã ãäœæããŠçããåŸãã§ããã...ã·ã¹ãã ã«ã®ã¿åœãŠã¯ãŸããŸãã
å®éãC ++æšæºã§ã¯ãcharã®charæåã¯æå®ãããŠããŸããã ãã®ãããcharã眲åãããŠããã³ã³ãã€ã©ã®å®è£ ãšãcharã眲åãããŠããªããã®ããããŸãã ãŸããããã¯ãå¥ã®ã·ã¹ãã ã®ã¢ã»ã³ããªåŸã«ããã°ã©ã ãåäœãæåŠããå¥ã®çç±ã§ãã
ããšãã°ããã®ã³ãŒãã¯Linux x86-64ã§ã¯æ£åžžã«åäœããŸãããLinux POWERã§ã¯åäœããŸããïŒGCCã§ããã©ã«ããã©ã¡ãŒã¿ã䜿çšããŠãã«ãããå ŽåïŒã
bool is_ascii( char s ) { return s >= 0; }
äžç¢ºå®æ§ãåãé€ãã«ã¯ãç®çã®åã«æ瀺çãªãã£ã¹ããè¿œå ããã ãã§ãã
bool is_ascii( char s ) { return static_cast<signed char>( s ) >= 0; }
ãã®äŸã§ã¯ããããæäœã®ã³ãŒããå®å šã«æžãæããããšãã§ããŸãã
bool is_ascii( char s ) { return s & 0x80 == 0; }
æååãã¥ãŒ
C ++æšæºã¯ãç¹å®ã®åŽé¢ãäœããã®æ¹æ³ã§èŠå¶ãããã®ã§ã¯ãªããåã³ã³ãã€ã©ãŒã¯ãã®è£éã§ãããã®åé¡ã解決ããŸãã
ããšãã°ãæååå®æ°ãã¡ã¢ãªå ã§ã©ã®ããã«è¡šçŸããããã«ã€ããŠã®ä¿èšŒã¯ãããŸããã
MSVSã³ã³ãã€ã©ã¯Windows-1251ã§æååå®æ°ããšã³ã³ãŒãããŸãããGCCã¯ããã©ã«ãã§UTF-8ã§æååå®æ°ããšã³ã³ãŒãããŸãã
ãã®ãããªéãããããããåãã³ãŒãã§ã¯ç°ãªãçµæãåŸãããŸããMSVSã§æ§ç¯ãããããã°ã©ã ã®strlenïŒ "Habr"ïŒã¯4ãè¿ããŸãã GCCã§-8ã
åãåé¡ã¯ãããŒã¿ã®å ¥åãšåºåã§ãçºçããŸãã ããšãã°ããã¹ãããã°ã©ã ã§ã¯ãããã€ãã®ããã¹ããã¡ã€ã«ã«ããŒã¿ãä¿åããŠèªã¿åãããšãã§ããŸãã
std::string readstr() { std::ifstream f( "file.txt" ); std::string s; std::getline( f, s ); return s; }
void writestr( const std::string& s ) { std::ofstream f( "file.txt" ); f.write( s.c_str(), s.size() ); }
ãããã®ãã¡ã€ã«ã1ã€ã®ç°å¢ã§åéãããã¢ããªã±ãŒã·ã§ã³ã«ãã£ãŠæžã蟌ãŸããèªã¿åãããéãããã¹ãŠãæ£åžžã«æ©èœããŸãã ãããããã®ãã¡ã€ã«ãWindowsã¢ããªã±ãŒã·ã§ã³ã«æžã蟌ãŸããLinuxã®äžã§ã¢ããªã±ãŒã·ã§ã³ãèªã¿åããšã©ããªããŸããïŒ..ãkrakozyabryããååŸããŸã:)
ãã®ãããªå Žåã¯ã©ãããã°ããã§ããïŒ èãããã解決çã®äžè¬ååã¯1ã€ã ãã§ããããã°ã©ã ã¡ã¢ãªã§æååãè¡šãçµ±äžãããæ¹æ³ãéžæããI / Oæã«æååã®æ瀺çãªãšã³ã³ãŒã/ãã³ãŒããè¡ãããšã§ãã å€ãã®éçºè ã¯ãããã°ã©ã ã§UTF-8ãšã³ã³ãŒãã£ã³ã°ã䜿çšããŠããŸãã ããã¯éåžžã«è¯ã解決çã§ãã
ããããåè¿°ããããã«ããåè»ãå šéåã§ä¿®çãããã³ãŒããäŸåããŠããäžå€æ¡ä»¶ã®äžéšãç Žãããšãã§ããŸããã§ããïŒæååãšã³ã³ãŒããWindows-1251ã§ããããšãèæ ®ããŠéçºãããŸããïŒã
- åºå®æåå¹ -ã€ã³ããã¯ã¹ã«ããæåãžã®ã©ã³ãã ã¢ã¯ã»ã¹ãå¯èœ
- ã³ãŒãã§ãã·ã¢èªã®æååå®æ°ãæžãããšãå¯èœã§ã
UTF-8ãšã³ã³ãŒãã£ã³ã°ã§ã¯ãæåã¯ç°ãªããã€ãæ°ã§è¡šãããšãã§ãããããæåã®èŠä»¶ãæºãããŠããŸããã UTF-8ã®å Žåã®2çªç®ã®èŠä»¶ã¯ãããšãã°ãæååå®æ°ãWindows-1251ã§ãšã³ã³ãŒããããŠããMSVC 2010ã§ã¯æºããããŠããŸããã ãã®ãããUTF-8ãæŸæ£ããå¿ èŠãããã...è¡ã衚瀺ãããŠãããšã³ã³ãŒãã£ã³ã°ããå®å šã«åãé¢ãããã¯ã€ãæååãã«åãæ¿ããããšã«ããŸããã
ãã®ãœãªã¥ãŒã·ã§ã³ã¯ãèŠä»¶ãã»ãŒå®å šã«æºãããŸããã
- ã»ãšãã©ãã¹ãŠã®UNIXã·ã¹ãã ã§ã¯ããã¯ã€ãã©ã€ã³ãã¯UTF-32ãšã³ã³ãŒãã£ã³ã°ã§è¡šãããŸããã€ãŸãããã®äžã®æåã®å¹ ã¯åºå®ãããã¿ã€ãwchar_tã®èŠçŽ ã®ãµã€ãºãšäžèŽããŸãã
- Windowsã¯UTF-16ã䜿çšããŸãã äžéšã®æåã¯ãµãã²ãŒããã¢ã§è¡šãããšãã§ããããããã®ãšã³ã³ãŒãã£ã³ã°ã§ã¯ç¶æ³ãããè€éã«ãªããŸãã ãããã幞ããªããšã«ãWindowsã¢ããªã±ãŒã·ã§ã³ãæ©èœããWindows-1251ã«ãããã¹ãŠã®ãã®ã¯ã2ãã€ãã·ãŒã±ã³ã¹ã§è¡šãããŸãã ãããã£ãŠãåæ段éã§ã¯ããµãã²ãŒããã¢ããŸã£ãããµããŒããããWindowsã§ã¯ãã¹ãŠã®æåãwchar_tåã®1ã€ã®èŠçŽ ã«åãŸããšä»®å®ããŸããã
- C ++ã§ã¯ããLããHelloãHabrïŒããªã©ã®ãã¯ã€ããæååå®æ°ãæå®ã§ããŸãã ãã®å Žåãã³ã³ãã€ã©ã¯ãã®è¡ããœãŒã¹ãã¡ã€ã«ã®ãšã³ã³ãŒãã£ã³ã°ããwchar_tãã¿ãŒã²ããã·ã¹ãã ã§è¡šããããšã³ã³ãŒãã£ã³ã°ã«å€æããåŠçãè¡ããŸãã
ããã«ããå¹ ã®åºãç·ãã䜿çšãããšãå€ãã®å©ç¹ãåŸãããŸããã
- æšæºCããã³C ++ã©ã€ãã©ãªã«ã¯ããã¯ã€ãæååããæäœããããã®å€ãã®é¢æ°ãšã¯ã©ã¹ããããŸããstrlenãstrstré¢æ°ãstd :: stringãstd :: stringstreamã¯ã©ã¹ãªã©ã«ç¬èªã®é¡äŒŒç©ãæžãå¿ èŠã¯ãããŸããã
- å€ãã®ãµãŒãããŒãã£ã©ã€ãã©ãªãã¯ã€ãæååããµããŒãããŠããŸãïŒããšãã°ãBOOSTïŒ
- ã»ãšãã©ã®WinAPIã¯ãã¯ã€ãã©ã€ã³ãã§åäœããŸã
å¿ èŠãªãã¹ãŠã®ãã©ãããã©ãŒã ã§ããã¯ã€ãæåãã¯Unicodeã§è¡šãããŸãã ãã®ãããã¢ããªã±ãŒã·ã§ã³ã¯ã©ãã³ã¢ã«ãã¡ããããšããªã«ã¢ã«ãã¡ãããã«éå®ãããªããªããäžçäžã®ããããèšèªããµããŒãããŸãã
å®éããšã³ã³ãŒãã£ã³ã°ã®æŠãã¯ã補åã移æ€ããäžã§æãå°é£ãªéšåã§ããã 圌女ã«ã€ããŠãã£ãšè©³ããç¥ãããšãã§ããŸã-次ã®èšäºã®ããã«ãããæ®ããŸããã:)
OSãã¡ã€ã«ã·ã¹ãã ã®æ©èœ
Windowsãã¡ã€ã«ã·ã¹ãã ã«ã¯ãã»ãšãã©ã®FS UNIXã©ã€ã¯ã·ã¹ãã ãšããã€ãã®éãããããŸãã
- 圌女ã¯å€§æåãšå°æåãåºå¥ããŸãã
- æåã\ãããã¹åºåãæåãšããŠäœ¿çšã§ããŸã
ããã¯äœã«ã€ãªãããŸããïŒ ããããŒãã¡ã€ã«ã«ãFiLe.Hããšããååãä»ããã³ãŒãã«ã#include <myfolder \ file.h>ããšèšè¿°ããŸãã Windowsã§ã¯ããã®ã³ãŒãã¯ã³ã³ãã€ã«ãããŸãããLinuxã§ã¯ããmyfolder \ file.hããšããååã®ãã¡ã€ã«ãèŠã€ãããªããšãããšã©ãŒã衚瀺ãããŸãã
ãããã幞ããªããšã«ããã®ãããªåé¡ãåé¿ããã®ã¯éåžžã«ç°¡åã§ã-ãã¡ã€ã«ã®åœåèŠåïŒããšãã°ããã¹ãŠã®ãã¡ã€ã«ã«å°æåãä»ããïŒãåãå ¥ãããããã«åºå·ããåžžã«ãã¹åºåãæåãšããŠã/ãã䜿çšããŸãïŒWindowsããµããŒãããŠããŸãïŒ
è¿·æãªãšã©ãŒãå®å šã«æé€ããããã«ãgitãªããžããªã«åçŽãªããã¯ãè¿œå ããincludeãã£ã¬ã¯ãã£ãããããã®ã«ãŒã«ã«æºæ ããŠãããã©ããã確èªããŸãã
ãŸããFSã®æ©èœã¯ã¢ããªã±ãŒã·ã§ã³èªäœã«åœ±é¿ããŸãã äŸãã°
std::string root_path = get_some_path(); std::string path = root_path + '\\' + fname;
éåžžã®æååé£çµæäœã§ãã¹ããåºå®ãããåºåãæåãšããŠã\ãã䜿çšããã³ãŒããããå Žåãäžéšã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯åºåãæåããã¡ã€ã«åã®äžéšãšããŠè§£éããããããå£ããŸãã
ãã¡ãããã/ãã䜿çšã§ããŸãããWindowsã§ã¯èŠãç®ãæªããäžè¬ã«ãä»ã®ã»ãã¬ãŒã¿ãŒã䜿çšãããOSããªããšããä¿èšŒã¯ãããŸããã
ãã®åé¡ã解決ããã«ã¯ãboost ::ãã¡ã€ã«ã·ã¹ãã ã©ã€ãã©ãªã䜿çšããŸãã çŸåšã®ã·ã¹ãã ã®ãã¹ãæ£ãã圢æã§ããŸãã
boost::filesystem::path root_path = get_some_path(); boost::filesystem::path path = root_path / fname;
ãããã«
C ++ã§ã®ã¯ãã¹ãã©ãããã©ãŒã ãœãããŠã§ã¢ã®éçºã¯ç°¡åãªäœæ¥ã§ã¯ãããŸããã ããŸããŸãªãœãããŠã§ã¢ããã³ããŒããŠã§ã¢ãã©ãããã©ãŒã ã§åäœããããã°ã©ã ãäœæããããã«ãè¿œå ã®åªåãããã«äœæããããšã¯ããããäžå¯èœã§ãã ãŸããC ++ã¯ã¯ãã¹ãã©ãããã©ãŒã èšèªã§ããã«ãããããããOSããã³ããŒããŠã§ã¢ã®ã³ã³ãã€ã©ã§å€æŽããã«æ£ããã¢ã»ã³ãã«ã§ããC ++ã®å€§èŠæš¡ããã°ã©ã ãéçºããããšã¯äžå¯èœã§ãã ãã ãããã®èšäºã§ç°¡åã«èª¬æããããã€ãã®ã«ãŒã«ãé å®ããŠããå Žåã¯ãå¿ èŠãªãã¹ãŠã®ãã©ãããã©ãŒã ã§å®è¡ãããã³ãŒããäœæã§ããŸãã ã¯ãããã®ããã°ã©ã ãæ°ããOSãŸãã¯ããŒããŠã§ã¢ã«ç§»è¡ããããšã¯ãããã»ã©é£ãããããŸããã
åèšãå¿ èŠãªã¯ãã¹ãã©ãããã©ãŒã ã³ãŒããäœæããã«ã¯ïŒ
- C ++æšæºãç解ãããã®äžã§èš±å¯ãããŠãããã®ãç解ããç¹å®ã®ã³ã³ãã€ã©ã®æ¡åŒµãšã¯äœããããã«ã¯æªå®çŸ©ã®åäœã«ã€ãªããããšãç解ããããšã¯æçã§ãã
- äžéšã®ã¯ã©ã¹ã§ãã©ãããã©ãŒã åºæã®ã³ãŒããã«ãã»ã«åããããšã«ãããã³ãŒãã§ã·ã¹ãã APIã䜿çšããããšãæåŠããããæ¢è£œã®ã¯ãã¹ãã©ãããã©ãŒã ã©ã€ãã©ãªã䜿çšããŸãã
- å
¥åã®éãã®å¯èœæ§ãèæ
®ããC ++æšæºã§ä¿èšŒãããŠããªãåºæ¬åã®ããããã£ã«äŸåããªãã§ãã ããã ãã®ããã«ãæšæºC ++ã©ã€ãã©ãªã®åºå®æ¬¡å
ã®åã䜿çšã§ããŸãã
- ããã°ã©ã ã¡ã¢ãªå
ã®æååãè¡šã圢åŒã決å®ããŸãã å€ãã®ãªãã·ã§ã³ããããŸãã ããšãã°ãå€ãã®ããã°ã©ã ã§è¡ãããŠããããã«UTF-8ã䜿çšãããããã¯ã€ããè¡ã«å®å
šã«åãæ¿ããŠã衚瀺è¡ã®åœ¢åŒãããŸã£ããæœè±¡åããŸãã
- ç°ãªããªãã¬ãŒãã£ã³ã°ã·ã¹ãã äžã®ãã¡ã€ã«ã·ã¹ãã ã®æ©èœãèæ ®ããŠãã ããïŒã³ãŒããïŒincludeãã£ã¬ã¯ãã£ããããã³ããã°ã©ã èªäœã®ããžãã¯ã®äž¡æ¹ïŒã
èè ïŒã¢ã¬ã¯ã»ã€ã»ã³ãããã