[C++] 字符串 string

字符串是 C++ 中常见数据类型,自带很多的函数:https://www.jianshu.com/p/c86d38db63ce

int 转 string

string s = to_string(12345);

string 转 int,long,long long

int x = stoi(str);
long x = stol(str);
long long x = stoll(str);

string 删除所有空格

// 方法一
str.erase(remove(str.begin(), str.end(), ' '), str.end());
// 方法二
int k = 0;
while((k = str.find(' ')) != -1) {
    
    
	str.erase(k, 1);
}
// 方法三
int k = 0;
while((k = str.find(' ')) != -1) {
    
    
	str.replace(k, 1, "");
}

猜你喜欢

转载自blog.csdn.net/weixin_43742643/article/details/128916101