This series of blog is my review of undergraduate study and reflection for C ++ at work, school work before only to find that C ++ is really too shallow.
Read other people's code, do not know how to re-familiar with the learning issue
First posted Code
void split(const string& s, vector<double>& sv, const char flag = ' ') {
sv.clear();
istringstream iss(s);
string temp;
while (getline(iss, temp, flag)) {
sv.push_back(stoi(temp));
}
return;
}
This string of code can be divided to a particular symbol such as "34613456-34" will be removed and that the numbers presented in the form of an array of int.
Used therein
1, the vector (the Vector) is a dynamic sequential container encapsulates the array size (Sequence Container). Need include <vector> incorporated
http://www.runoob.com/w3cnote/cpp-vector-container-analysis.html
2, stoi () is converted into a string of numbers embodiment int, int of substandard or beyond the range of the number of throws an exception, which is contained in the std, another function atoi () but not beyond the same effect Int range throws an exception, but returns upper and lower bounds, it is in the standard library. Function prototype is as follows:
int stoi (const string& str, size_t* idx = 0, int base = 10);
among them:
str: String object represents an integer.
idx: a pointer pointing to an object of type size_t, set its value to the next value str character position by the function. This parameter may be a null pointer, it is not used in this case.
base: to determine the numerical basis for valid characters and their interpretation (base).
If 0, the base is used is determined by the sequence format (for details, see strtol). Please note that, by default, this parameter is 10, not 0.
Such common functions further comprising:
(1) stol Converts a string to long int
(2) stoul converting the string to an unsigned integer stoll, stoull
(3) stof convert the string to float
(. 4) STOD convert the string to Double STOLD
3, getline function prototype:
getline(char *sink, streamsize size, char delimiter='\n')
getline( istream &is, string str, char delimiter );
So this method can not only read one line, also designated as the character flag is read, as in the above example code is 'used' spaces
4, istringstream contained in the <sstream> in (<sstream> comprising <iostream> so no contains both)
1 istringstream istream derived from reading data from a string
2 ostringstream write ostream derived from a string to
3 stringstream iostream derived from the read or write into the string from the string
Here is a character string used as the stream, and output operators can accept arguments include any built-in data types const char *, so as Code:
string str="this is a test";
istringstream is(str);
string s;
while(is>>s)
{
cout<<s<<endl;
}
The output will be:
this
is
a
test
But also a data type conversion can be done using this method:
int a, b, c;
string s = "2 34 564";
istringstream ss(s);
ss >> a >> b >> c;
cout << a << " " << b << " " << c << " ";
The output is 234 564