{"id":92,"date":"2018-12-26T02:35:59","date_gmt":"2018-12-26T05:35:59","guid":{"rendered":"http:\/\/35.243.195.209\/?p=92"},"modified":"2018-12-26T02:35:59","modified_gmt":"2018-12-26T05:35:59","slug":"string-in-c","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2018\/12\/26\/string-in-c\/","title":{"rendered":"String in C++"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce the <code>string<\/code> class in c++ STL. It covers useful APIs and some of my recommendations.<br \/>\n1. <a href=\"#Split\">Split<\/a><br \/>\n2. <a href=\"#Access\">Access<\/a><br \/>\n3. <a href=\"#Comparison\">Comparison<\/a><br \/>\n4. <a href=\"#Numeric-Conversions\">Numeric-Conversions<\/a><\/p>\n<h2>Details<\/h2>\n<p><a name=\"Split\"><\/a><\/p>\n<h3>Split<\/h3>\n<p>Thanks to <a href=\"&quot;https:\/\/www.fluentcpp.com\/2017\/04\/21\/how-to-split-a-string-in-c\/&quot;\">this post<\/a>, we present the conlusions for <code>split<\/code> first.<br \/>\n1. If you have access to <a href=\"&quot;https:\/\/www.google.com\/search?q=boost+c%2B%2B&amp;oq=boost+c%2B%2B&amp;aqs=chrome.0.0j69i60l3j0l2.5993j1j9&amp;sourceid=chrome&amp;ie=UTF-8&quot;\">boost<\/a>, please do Solution 1.<br \/>\n2. You do not want to include other libraries. Implement own algorithms using Solution 2.<br \/>\n3. Use stream and see Solution 3.<\/p>\n<h4>Solutions<\/h4>\n<h5>1. boost<\/h5>\n<pre><code class=\"c\">#include &lt;boost\/algorithm\/string.hpp&gt;\n\nstd::string text = \"Let me split this into words\";\nstd::vector&lt;std::string&gt; results;\n\nboost::split(results, text, [](char c){return c == ' ';});\n<\/code><\/pre>\n<p>Boost::split essentially performs multiple find_if on the string on the delimiter, until reaching the end.<\/p>\n<p>It allows even <code>several different delimiters<\/code> and is 60% faster than solution 3.<\/p>\n<h5>2. getline<\/h5>\n<pre><code class=\"c\">char delimiter = ',';\nstd::vector&lt;std::string&gt; tokens;\nstd::string token;\nstd::istringstream tokenStream(s);\nwhile (std::getline(tokenStream, token, delimiter))\n{\n    tokens.push_back(token);\n}\n<\/code><\/pre>\n<h5>3. stream<\/h5>\n<pre><code class=\"c\">std::string text = \"Let me split this into words\";\n\nstd::istringstream iss(text);\nstd::vector&lt;std::string&gt; results(std::istream_iterator&lt;std::string&gt;{iss},\n                                 std::istream_iterator&lt;std::string&gt;()); \/\/ only works for space\n<\/code><\/pre>\n<p><a name=\"Access\"><\/a><\/p>\n<h3>Access<\/h3>\n<p>Actually <code>string<\/code> acts like <code>vector&lt;char&gt;<\/code>, we recommend you use <code>.at()<\/code> rather than <code>[]<\/code>, since <code>.at()<\/code> check the boundary and throw exceptions.<\/p>\n<pre><code class=\"c\">string str = \"hello\";\ncout &lt;&lt; str.at(100) &lt;&lt; endl; \/\/ libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string\ncout &lt;&lt; str[100] &lt;&lt; endl; \/\/ exit normally, print something you do not really know\n<\/code><\/pre>\n<p><a name=\"Comparison\"><\/a><\/p>\n<h3>Comparison<\/h3>\n<p>We can use <code>!=<\/code> and <code>==<\/code> directly.<\/p>\n<p>For <code>&lt;<\/code> and <code>&gt;<\/code>, <\/p>\n<table>\n<thead>\n<tr>\n<th>value<\/th>\n<th>relation between <em>compared string<\/em> and <em>comparing string<\/em><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&lt; 0<\/td>\n<td>Either the value of the first character that does not match is lower in the <em>compared string<\/em>, or all compared characters match but the <em>compared string<\/em> is shorter.<\/td>\n<\/tr>\n<tr>\n<td>> 0<\/td>\n<td>Either the value of the first character that does not match is greater in the <em>compared string<\/em>, or all compared characters match but the <em>compared string<\/em> is longer.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"c\">\"abc\" &lt; \"abcd\"; \/\/ true\n\"abc\" &lt; \"bbc\" ; \/\/ true\n<\/code><\/pre>\n<p><a name=\"Numeric-Conversions\"><\/a><\/p>\n<h3>Numeric Conversions<\/h3>\n<p>The functions throw a std::invalid_argument exception if the conversion is not possible and a std::out_of_range exception if the determined value is too big for the destination.<\/p>\n<pre><code class=\"c\">#include &lt;cfloat&gt;\ncout &lt;&lt; INT_MAX &lt;&lt; endl;\ncout &lt;&lt; DBL_MAX &lt;&lt; endl;\ncout &lt;&lt; FLT_MAX &lt;&lt; endl;\n\/*\n2147483647\n1.79769e+308\n3.40282e+38\n*\/\n<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th><strong>Method<\/strong><\/th>\n<th><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>std::to_string(val)<\/code><\/td>\n<td>Converts <code>val<\/code> into a <code>std::string<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stoi(str)<\/code><\/td>\n<td>Returns an <code>int<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stol(str)<\/code><\/td>\n<td>Returns a <code>long<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stoll(str)<\/code><\/td>\n<td>Returns a <code>long long<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stoul(str)<\/code><\/td>\n<td>Returns an <code>unsigned long<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stoull(str)<\/code><\/td>\n<td>Returns an <code>unsigned long long<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stof(str)<\/code><\/td>\n<td>Returns a <code>float<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stod(str)<\/code><\/td>\n<td>Returns a <code>double<\/code> value.<\/td>\n<\/tr>\n<tr>\n<td><code>std::stold(str)<\/code><\/td>\n<td>Returns an <code>long double<\/code> value.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce the string class in c++ STL. It covers useful APIs and some of my recommendations. 1. Split 2. Access 3. Comparison 4. Numeric-Conversions Details Split Thanks to this post, we present the conlusions for split first. 1. If you have access to boost, please do Solution 1. 2&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":135,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions\/135"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}