1. Input of getline (cin, str)
For example, the title may require an integer to be input first and then one or more strings containing spaces. Examples of the test (ps: The following discussion is based on C++4.9):
C++ getline function code implementation code:2 abc haha
Enter 2 and return. Unfortunately, the following strange output is immediately obtained:int main(){ int n; string str1,str2; cin>>n; getline(cin,str1); cout<<n<<endl<<str1<<endl; }
2 2 -------------------------------- Process exited after 2.231 seconds with return value 0 Press any key to continue....
cout<<n<<" "<<(str1=="")<<endl; //After entering 2 and returning, output the following results 2 1
How can we solve this problem? It's simple to absorb line breaks before the getline function reads them.
The correct results are printed as follows:int main(){ int n; string str1,str2; cin>>n; cin.get();//Line breaks after absorbing input n ahead of time getline(cin,str1); cout<<n<<" "<<str1<<endl; }
Summary: If the getline function is preceded by the CIN > operator (except getline), be sure to use the input stream to absorb the newline character first.2 abc haha 2 abc haha
2. Replacement of strings
C++ provides only a few basic position-based string replace ment functions, such as
But in many cases, the string s2 is used to replace the S1 in the string s1. Of course, you can use the replacement provided by STL to implement indirectly.//Replace a character whose length is len from the beginning of pos with str string& replace (size_t pos, size_t len, const string& str);
//Replace s1 with s2 in the string s string replaceAll(string s,const string &s1,const string &s2){ int pos,len=s1.length(); while((pos=s.find(s1))!=-1){ s=s.replace(pos,len,s2); } return s; }
3. String Conversion Tool sstream
In some topics, it is often necessary to convert int or double to string type or string type to double type. At this time, it is very easy to implement using sstream, such as using sstream to achieve the work of to_string. (c++11 has been implemented internally)
Here's a look at the string-to-double conversionstring to_string(double v){ stringstream ss;//1. Create a string stream stream stream string str; ss<<v; //2. assign v of type int to ss ss>>str; //3. Assign content to str return str; }
double string2double(string s){ stringstream ss(s);//1. Create a string stream stream stream and accept the string double v; //At this point ss is like a cin, which can be type matched ss>>v; //Read v from ss stream. return v; }
4. String segmentation
String segmentation is often encountered in string processing. Unfortunately, C++ does not provide split function, but it can also be implemented indirectly:
//Segmenting string s according to string c to get vector_v vector<string> split(const string& s, const string& c){ vector<string> v; int pos1=0,pos2; while((pos2=s.find(c,pos1))!=-1){ v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size(); } if(pos1 != s.length()) v.push_back(s.substr(pos1)); return v; }
The above code is more general, but because the find function is naive O(n^2). If the splitter is a single character such as''(or the same number such as''), sstream is more efficient. Now there is the following requirement: in the string "1.23 # 2.01 # 0.50" to find the sum of floating-point numbers, that is, 1.23 + 2.01 + 0.50. stringstream s s (s) mentioned above that when s s receives the string s, s s is equivalent to a CIN input stream that automatically matches the input type. Therefore, the'#'can be replaced by the end partitioner' t' or the space that ss(ss is equivalent to cin) can recognize. Then a double v is defined and read directly from SS > v.
int main(){ string s="1.23#2.01#0.50"; for(int i=0;s[i];i++) if(s[i]=='#') s[i]=' ';//Replace'#'with spaces stringstream ss(s);//Create a string stream stream stream and accept the string s double v,sum=0; while(ss>>v){//Automatic type matching sum+=v; } cout<<sum<<endl;//3.74 }