{"id":178,"date":"2014-07-09T02:15:50","date_gmt":"2014-07-09T11:15:50","guid":{"rendered":"http:\/\/blog.box.kr\/?p=178"},"modified":"2014-07-09T02:15:50","modified_gmt":"2014-07-09T11:15:50","slug":"stdmap-example","status":"publish","type":"post","link":"https:\/\/blog.box.kr\/?p=178","title":{"rendered":"std::map example"},"content":{"rendered":"<pre style=\"color: black;\"><span style=\"font-style: italic; color: #808080;\">\/\/ std::map example<\/span>\n<span style=\"font-style: italic; color: #808080;\">\/\/ opposite words<\/span>\n\n<span style=\"color: #008000;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #008000;\">#include &lt;map&gt;<\/span>\n<span style=\"color: #008000;\">#include &lt;string&gt;<\/span>\n<span style=\"font-weight: bold;\">using<\/span> <span style=\"font-weight: bold;\">namespace<\/span> std;\n\n<span style=\"font-weight: bold;\">typedef<\/span> std::map&lt;std::string, std::string&gt; TStrStrMap;\n<span style=\"font-weight: bold;\">typedef<\/span> std::pair&lt;std::string, std::string&gt; TStrStrPair;\n\n<span style=\"color: #800000;\">int<\/span> main(<span style=\"color: #800000;\">int<\/span> argc, <span style=\"color: #800000;\">char<\/span> *argv[])\n{\n\tTStrStrMap tMap;\n\n\ttMap.insert(TStrStrPair(<span style=\"color: #dd0000;\">\"yes\"<\/span>, <span style=\"color: #dd0000;\">\"no\"<\/span>));\n\ttMap.insert(TStrStrPair(<span style=\"color: #dd0000;\">\"up\"<\/span>, <span style=\"color: #dd0000;\">\"down\"<\/span>));\n\ttMap.insert(TStrStrPair(<span style=\"color: #dd0000;\">\"left\"<\/span>, <span style=\"color: #dd0000;\">\"right\"<\/span>));\n\ttMap.insert(TStrStrPair(<span style=\"color: #dd0000;\">\"good\"<\/span>, <span style=\"color: #dd0000;\">\"bad\"<\/span>));\n\n\tstd::string s;\n\tstd::cout &lt;&lt; <span style=\"color: #dd0000;\">\"Enter word: \"<\/span> &lt;&lt; std::endl;;\n\tstd::cin &gt;&gt; s;\n\n\n\tstd::string strValue = tMap[s];\n\t<span style=\"font-weight: bold;\">if<\/span>(strValue!=<span style=\"color: #dd0000;\">\"\"<\/span>)\n\t{\n\t\t<span style=\"font-style: italic; color: #808080;\">\/\/ Show value<\/span>\n\t\tstd::cout &lt;&lt; <span style=\"color: #dd0000;\">\"Opposite: \"<\/span> &lt;&lt; strValue &lt;&lt; endl;\n\t}\n\t<span style=\"font-weight: bold;\">else<\/span>\n\t{\n\t\tTStrStrMap::iterator p;\n\t\t<span style=\"color: #800000;\">bool<\/span> bFound=<span style=\"font-weight: bold;\">false<\/span>;\n\t\t<span style=\"font-style: italic; color: #808080;\">\/\/ Show key<\/span>\n\t\t<span style=\"font-weight: bold;\">for<\/span>(p = tMap.begin(); p!=tMap.end(); ++p)\n\t\t{\n\t\t\tstd::string strKey;\n\t\t\tstrValue = s;\n\t\t\tstrKey= p-&gt;second;\n\t\t\t<span style=\"font-weight: bold;\">if<\/span>( strValue  == strKey)\n\t\t\t{\n\t\t\t\t<span style=\"font-style: italic; color: #808080;\">\/\/ Return key<\/span>\n\t\t\t\tstd::cout &lt;&lt; <span style=\"color: #dd0000;\">\"Opposite: \"<\/span> &lt;&lt; p-&gt;first &lt;&lt; std::endl;\n\t\t\t\tbFound = <span style=\"font-weight: bold;\">true<\/span>;\n\t\t\t}\n\t\t}\n\t\t<span style=\"font-style: italic; color: #808080;\">\/\/ If not found opposite word<\/span>\n\t\t<span style=\"font-weight: bold;\">if<\/span>(!bFound)\n\t\t{\n\t\t\tstd::cout &lt;&lt; <span style=\"color: #dd0000;\">\"Word not in map.\"<\/span> &lt;&lt; std::endl;\n\t\t}\n\t}\n\t<span style=\"font-weight: bold;\">return<\/span> <span style=\"color: #0000ff;\">0<\/span>;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ std::map example \/\/ opposite words #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;string&gt; using namespace std; typedef std::map&lt;std::string, std::string&gt; TStrStrMap; typedef std::pair&lt;std::string, std::string&gt; TStrStrPair; int main(int argc, char *argv[]) { TStrStrMap tMap; tMap.insert(TStrStrPair(&#8220;yes&#8221;, &#8220;no&#8221;)); tMap.insert(TStrStrPair(&#8220;up&#8221;, &#8220;down&#8221;)); tMap.insert(TStrStrPair(&#8220;left&#8221;, &#8220;right&#8221;)); tMap.insert(TStrStrPair(&#8220;good&#8221;, &#8220;bad&#8221;)); std::string s; std::cout &lt;&lt; &#8220;Enter word: &#8221; &lt;&lt; std::endl;; std::cin &gt;&gt; s; std::string strValue = tMap[s]; if(strValue!=&#8221;&#8221;) { \/\/ Show value std::cout &lt;&lt; &#8220;Opposite: &#8221; &lt;&lt; strValue &lt;&lt; endl; } else { TStrStrMap::iterator p; bool bFound=false; \/\/ Show key for(p = tMap.begin(); p!=tMap.end(); ++p) { std::string strKey; strValue = s; strKey= p-&gt;second; if( strValue == strKey) { \/\/ Return key std::cout &lt;&lt; &#8220;Opposite: &#8221; &lt;&lt; p-&gt;first &lt;&lt; std::endl; bFound = true; } } \/\/ If not found opposite word if(!bFound) { std::cout &lt;&lt; &#8220;Word not in map.&#8221; &lt;&lt; std::endl; } } return 0; }<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"ngg_post_thumbnail":0,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[9,7],"tags":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5q9Zn-2S","jetpack-related-posts":[{"id":335,"url":"https:\/\/blog.box.kr\/?p=335","url_meta":{"origin":178,"position":0},"title":"\uc815\uaddc\ud45c\ud604\uc2dd(Regular Expression) on C++","date":"2014-08-11","format":false,"excerpt":"*\u00a0C++ Regex 2011\ub144 \uacbd C++\uc758 \uc0c8\ub85c\uc6b4 \ud45c\uc900 C++11 (C++0x)\uc744 \uc815\uc758\ud558\ub294 \uacfc\uc815\uc5d0\uc11c Boost.Regex\uac00 \ud45c\uc900\uc73c\ub85c \ud3ec\ud568\ub418\uc5c8\ub2e4. C++11\uc758 \uc815\uaddc\ud45c\ud604\uc2dd \ub77c\uc774\ube0c\ub7ec\ub9ac\ub294 <regex> \ud5e4\ub354\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4.\uc774\ub85c \uc778\ud574 \ubcc4\ub3c4\uc758 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc124\uce58\ud558\uc9c0 \uc54a\uace0\ub3c4 \uc190\uc27d\uac8c \uc815\uaddc\ud45c\ud604\uc2dd \uae30\ub2a5\uc744 \ud65c\uc6a9\ud560 \uc218 \uc788\uac8c \ub418\uc5c8\ub2e4. \ucd5c\uc2e0 \ubc84\uc804\uc758 Visual C++, GNU C++ \ucef4\ud30c\uc77c\ub7ec\ub294 std::regex\ub97c \ud3ec\ud568\ud55c C++11 \ud45c\uc900\uc774 \uc815\uc758\ud55c \ub300\ubd80\ubd84\uc758 \uae30\ub2a5\uc744 \uc9c0\uc6d0\ud55c\ub2e4. \ucef4\ud30c\uc77c\ub7ec\ubcc4 C++11\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":230,"url":"https:\/\/blog.box.kr\/?p=230","url_meta":{"origin":178,"position":1},"title":"std::string \ub2e4\ub8e8\uae30.. ( \ucc3e\uae30, \uacf5\ubc31\uc81c\uac70 \ub4f1\ub4f1)","date":"2014-07-23","format":false,"excerpt":"\ubb38\uc790\uc5f4 string #include \"StdAfx.h\" #include \"StringTest.h\" #include <algorithm> \/\/transform \uc0ac\uc6a9 #include <functional> \/\/bind1st using std::string; \/\/ \uc774\ub807\uac8c \uc9c0\uc815\ud574\uc900\ub2e4. StringTest::StringTest() { log(\"StringTest \uc0dd\uc131\uc790 \ud638\ucd9c\"); } StringTest::~StringTest(void) { log(\"StringTest \uc18c\uba78\uc790 \ud638\ucd9c\"); } void StringTest::test() { log(\"============ string class ==========\"); \/\/ \uc0dd\uc131\uc790 \u00a0 std::string s0; \/\/ \ube44\uc5b4\uc788\ub294 string std::string s1(\"012345\",3); \/\/ \uacb0\uacfc: 012\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":339,"url":"https:\/\/blog.box.kr\/?p=339","url_meta":{"origin":178,"position":2},"title":"[c++11] \uc798 \uc4f0\uba74 \ub9e4\uc6b0 \ud3b8\ub9ac\ud55c C++11\uc758 \ubb38\ubc95\ub4e4","date":"2014-08-12","format":false,"excerpt":"C\/C++\uc5b8\uc5b4\ub294 \uc800\uc218\uc900\uae4c\uc9c0 \uc815\ubc00\ud558\uac8c \ub2e4\ub8f0 \uc218 \uc788\ub294 \ubc18\uba74, \ud504\ub85c\uadf8\ub798\uba38\uac00 \uc77c\uc77c\ud788 \uad00\ub9ac\ud574\uc918\uc57c\ud558\ub294 \ubd80\ubd84\uc774 \ub9ce\ub2e4\ub294\uac8c C\/C++\uc5b8\uc5b4\uc5d0 \ub300\ud55c \uc77c\ubc18\uc801\uc778 \uacac\ud574\uc785\ub2c8\ub2e4. \uadf8\ub798\uc11c \uc800\uc218\uc900\uae4c\uc9c0 \uc77c\uc77c\ud788 \uc2e0\uacbd\uc4f0\uc9c0 \uc54a\uc544\ub3c4 \ub418\ub294 \ucfe8\ud55c \uc5b8\uc5b4\ub97c \uac00\uc9c0\uace0 \ub192\uc740 \uc0dd\uc0b0\uc131\uc744 \ucd94\uad6c\ud558\ub294\uac8c \ucd94\uc138\uc774\uae30\ub3c4 \ud569\ub2c8\ub2e4. . . . \ub9cc, C++\uc758 \uace8\uc218\ube60\ub85c\uc11c C++11\uc744 \ud1b5\ud574 C++\ub85c\ub3c4 \ucda9\ubd84\ud788 \ucfe8\ud558\uace0 \uc0dd\uc0b0\uc131 \ub192\uc740 \ucf54-\ub4dc\ub97c \uc791\uc131\ud560\uc218 \uc788\ub2e4\uace0 \ubcc0\ud638\ub97c \ud574\ubcf4\ub824\uace0 \ud569\ub2c8\ub2e4. Visual\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":220,"url":"https:\/\/blog.box.kr\/?p=220","url_meta":{"origin":178,"position":3},"title":"VARIANT to std::string","date":"2014-07-23","format":false,"excerpt":"Here's one way, not necessarily the best. In general, std::string doesn't play very well with VARIANT. #include <comutil.h> #include <string> std::string from_variant(VARIANT& vt) { _bstr_t bs(vt); return std::string(static_cast<const char*>(bs)); } void to_variant(const std::string& str, VARIANT& vt) { _bstr_t bs(str.c_str()); reinterpret_cast<_variant_t&>(vt) = bs; } \u00a0 \ub610\ud55c.. VARIANT\uac00 \ube44\uc5b4 \uc788\ub294\uc9c0 \ud655\uc778 \ud558\ub294\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":143,"url":"https:\/\/blog.box.kr\/?p=143","url_meta":{"origin":178,"position":4},"title":"Splitting a C++ std::string using tokens, e.g. \u201c;\u201d [duplicate]","date":"2014-06-29","format":false,"excerpt":"std::vector<std::string> split(const std::string& s,char seperator){ std::vector<std::string> output; std::string::size_type prev_pos =0, pos =0;while((pos = s.find(seperator, pos))!= std::string::npos){ std::string substring( s.substr(prev_pos, pos-prev_pos)); output.push_back(substring); prev_pos =++pos;} output.push_back(s.substr(prev_pos, pos-prev_pos));\/\/ Last wordreturn output;}","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":337,"url":"https:\/\/blog.box.kr\/?p=337","url_meta":{"origin":178,"position":5},"title":"\uc815\uaddc\uc2dd \uc0ac\uc6a9\ubc95 [\ud38c] boost \uc0ac\uc6a9","date":"2014-08-12","format":false,"excerpt":"\uc9c8\ubb38 ::#\uc774 \ubd99\uc740 \uc804\ud654 \ubc88\ud638 \uc55e\uc5d0\ub294 %23\uc744 \ubd99\uc774\uace0 \uc2f6\uc74c.. \u00a0 \ub2f5\ucf54\ub4dc :: std::string result, list = \"#011-222-3333\"; const char* regStr = \"(#)?(01[016789])-?(d{3,4})-?(d{4})\"; const char* subStr = \"(?1%23)$2$3$4\"; boost::regex_replace(result, list.begin(), list.end(), regStr, subStr, boost::match_default | boost::format_all); std::cout << result << std::endl; [url]http:\/\/www.boost.org\/libs\/regex\/doc\/regex_replace.html[\/url] [url]http:\/\/ourworld.compuserve.com\/homepages\/John_Maddock\/[\/url] [url]http:\/\/research.microsoft.com\/projects\/greta\/gretauserguide.htm[\/url] \u00a0","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/178"}],"collection":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":0,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}