Fundamental Chapter: 9)C++ String sstream (String)

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):

2
abc haha
C++ getline function code implementation code:
int main(){
	int n;
	string str1,str2;
	cin>>n;
	getline(cin,str1);
	cout<<n<<endl<<str1<<endl;
}
Enter 2 and return. Unfortunately, the following strange output is immediately obtained:
2
2


--------------------------------
Process exited after 2.231 seconds with return value 0
 Press any key to continue....
Why is this so? Where is the input abc haha going? In fact, the getline() function takes the newline character as the input end flag, and it does not ignore the newline character at the beginning of the line. In other words, the return typed after the above input 2 has been received by getline, but 2 has been assigned to n, while str1 has received an empty string. Continue to use the previous test cases. Verify with the following code:
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.

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;
}
The correct results are printed as follows:

2
abc haha
2 abc haha

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. Replacement of strings

C++ provides only a few basic position-based string replace ment functions, such as

 //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); 
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 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)
string 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; 
} 
Here's a look at the string-to-double conversion
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
}

Posted by Teach on Tue, 02 Apr 2019 17:48:30 -0700