类型转换
string 转 int
How can I convert a std::string to int?
Easiest way to convert int to string in C++
In C++11 there are some nice new convert functions from std::string to a number type.
So instead of
- string->int
1
atoi( str.c_str() )
you can use
- string->int
1
std::stoi( str )
where str is your number as std::string.
There are version for all flavours of numbers:1
long stol(string), float stof(string), double stod(string),...
- int->string
1
std::string s = std::to_string(42);
see http://en.cppreference.com/w/cpp/string/basic_string/stol
文件操作
写文件
1 | // writing on a text file |
读文件
1 | // reading a text file |
字符串操作
字符串split
Parse (split) a string in C++ using string delimiter (standard C++)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17std::string s = "scott>=tiger>=mushroom";
std::string delimiter = ">=";
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;
// Output:
scott
tiger
mushroom
md5代码
1 | #include <openssl/md5.h> |
进制转换
16进制字符串转为10进制数字
1 | uint64_t number; |