string操作
字符串find的使用
s1.find(s2) 可以找到s1中存在s2子串的位置
如果找不到则返回npos
find(查找的字符串,位置) 如果添加位置可以指定字符串从位置开始查找
如果需要多次查找相同字符串不同的位置可以操作如下
size_t pos = s.find(st);
while(pos != string::npos) {
pos = s.find(st, pos + 1); // 继续从下一个位置查找
}
字符串转int
string s = "09:41";
int hour = stoi(s.substr(0, 2));
int minute = stoi(s.substr(3, 2));
字符串转float
string s = "1.3235";
int num = stof(s);
基本类型转string
int num = 233;
string str = to_string(num);

浙公网安备 33010602011771号