c++用符号分割字符串string并转成数字
--->先科普一下C++里面replace()函数的用法
replace函数包含于头文件#include <string>当中,泛型算法replace把队列中与给定值相等的所有值替换成另一个值.
整个队列都被扫描,即此算法的各个版本都在线性时间内执行,其复杂度为O(n)。
replace的执行要遍历由区间[first, last)限定的整个队列,以把old_value替换成new_value。下面说一说replace的十种用法。
(1)用str替换指定字符串从起始位置pos开始长度为len的字符
string& replace(size_t pos, size_t len, const string& str);
代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
str = str.replace(str.find("a"), 2, "#");
//从第一个a位置开始的两个字符替换成#
cout << str << endl;
return 0;
}
#结果:he is@ # good boy
(2)用str替换迭代器起始位置和结束位置的字符
string& replace(const_iterator i1, const_iterator i2, const string& str)
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
str = str.replace(str.begin(), str.begin()+5, "#");
#用#替换从beigin位置开始的5个字符
cout << str << endl;
return 0;
}
#结果:#@ a@ good boy
(3)用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
string& replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
string substr = "12345";
str = str.replace(0, 5, substr, substr.find("1"), 4);
//用substr的指定字符串替换str指定字符串
cout << str << endl;
return 0;
}
#结果:1234@ a@ good boy
(4)string转char*时编译器可能会报出警告,不建议这样做。用str替换从指定位置0开始长度为5的字符串。
string& replace(size_t pos, size_t len, const char* s);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
char* str1 = "12345";
str = str.replace(0, 5, str1);
//用str替换从指定位置开始长度为5的字符串
cout << str << endl;
return 0;
}
#结果:12345@ a@ good boy
(5)string转char*时编译器可能会报出警告,不建议这样做。用str替换从指定迭代器位置的字符串
string& replace(const_iterator i1, const_iterator i2, const char* s);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
char* str1 = "12345";
str = str.replace(str.begin(), str.begin()+6, str1);
//用str替换从指定迭代器位置的字符串
cout << str << endl;
return 0;
}
结果:12345 a@ good boy
(6)string转char*时编译器可能会报出警告,不建议这样做。
用s的前n个字符替换从开始位置pos长度为len的字符串
string& replace(size_t pos, size_t len, const char* s, size_t n);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
char* str1 = "12345";
str = str.replace(0, 6, str1, 4);
//用str1的前4个字符串替换从位置0-6的字符串
cout << str << endl;
return 0;
}
结果:12345 a@ good boy
(7)string转char*时编译器可能会报出警告,不建议这样做。
用s的前n个字符替换指定迭代器位置(从i1从i2)的字符串
string& replace(const_iterator i1, const_iterator i2, const char* s, size_t n);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
char* str1 = "12345";
str = str.replace(str.begin(), str.begin()+6, str1, 4);
//用str1的前4个字符串替换从位置0-6的字符串
cout << str << endl;
return 0;
}
结果:1234 a@ good boy
(8)用重复n次的c字符替换从指定位置pos长度为len的内容
string& replace(size_t pos, size_t len, size_t n, char c);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
char str1 = '#';
str = str.replace(0, 6, 3, str1);
//用重复3次的str1字符替换从位置0-6的字符串
cout << str << endl;
return 0;
}
结果:### a@ good boy
(9)用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容
string& replace(const_iterator i1, const_iterator i2, size_t n, char c);
#代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
char str1 = '#';
str = str.replace(str.begin(), str.begin()+6, 3, str1);
//用重复3次的str1字符替换从位置0-6的字符串
cout << str << endl;
return 0;
}
结果:### a@ good boy
(10)
replace(str1.begin(), str1.end(), ',', ' '); //逗号替换空格
c++用符号分割字符串string并转成数字第一种方案:
在学习c++中string相关基本用法的时候,发现了sstream中的istringstream可以将字符串类似控制台的方式进行输入,而实质上这个行为等同于利用空格将一个字符串进行了分割,于是考虑到可以利用这个特征来实现c++库函数中没有的字符串分割函数split。
string src("Avatar 123 5.2 titanic k");
istringstream istrStream(src);//建立src到istrStream的联系
string s1, s2;
int n;
double d;
char c;
istrStream >> s1 >> n >> d >> s2 >> c;
完整代码如下:
#include <iostream>
#include <string>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
string* split(int& length, string str, const char token=' ')
{
const char SPACE = 0;
//(1)
/*
需要进行数据透明处理:由于istringstream会像cin一样,把空格视为数据间的界限
所以当分隔符不是空格的时候,需要将传入的分隔符换为空格
并且要提前对原有空格进行数据透明处理
*/
if(token != ' ')
{
//先把原有的空格替换成ASCII中的不可见字符
replace(str.begin(), str.end(), ' ', SPACE);
//再把分隔符换成空格,交给字符串流处理
replace(str.begin(), str.end(), token, ' ');
/*
假设输入字符换为:"a b,c,d,e,f g"
分隔符为非空格:','
则被替换成:"aSPACEb c d r fSPACEg"
*/
}
//(2)进行数据分割
//实例化一个字符串输入流,输入参数即待处理字符串
istringstream i_stream(str);
//将length置0
length = 0;
//可以用队列queue也可以用vector<string>数组
queue<string> q;//用一个string实例s接收输入流传入的数据,入队并计数
string s;
while(i_stream >> s)
{
q.push(s);
length++;
}
//(3)数组生成
//根据计数结果动态开辟一个字符串数组空间
string* results = new string[length];
//将队列中的数据转入数组中
for(int i = 0; i < length; i++)
{
results[i] = q.front();
q.pop();
if(token!=' ')
{
replace(results[i].begin(), results[i].end(), SPACE, ' ');
}
}
return results;
}
int main()
{
int length;
string* results = split(length, "a b,c,d,e,f,g", ',');
for(int i = 0; i < length; i++)
{
cout << results[i] << endl;
}
return 0;
}
————————————————————————————————————————————
这里介绍C++里面int与string的相互转换
(1)c++11标准增加了全局函数std::to_string
#include <iostream>
#include <string>
int main()
{
string pi = "pi is " + to_string(3.1415926);
string perfect = to_string(1+2+4+7+14) + "is a perfect number";
cout << pi << endl;
cout << perfect << endl;
return 0;
}
下面是to_string()函数的实现
#include <iostream>
#include <string>
using namespace std;
#define max 100
string to_String(int n)
{
int m = n;
char s[max];
char ss[max];
int i = 0, j = 0;
if(n < 0)//处理负数
{
m = 0 - m;
j = 1;
ss[0] = '-';
}
while(m > 0)
{
s[i++] = m % 10 + '0';
m /= 10;
}
s[i] = '\0';
i = i - 1;
while(i >= 0)
{
ss[j++] = s[i--];
}
ss[j] = '\0';
return ss;
}
还有另一种方法是借助字符串流,标准库定义了三种类型字符串流:istringstream, ostringstream, stringstream,看名字就知道这几种类型和iostream的几个非常类似,分别可以读、写、以及读和写string类型,它们也确实是从iostream类型派生而来的。要使用它们需要包含sstream头文件。
int aa = 30;
stringstream ss;
ss << aa;
string s1 = ss.str();
cout << s1 << endl;
————————————————————————————————————————————
把string转换成int
可以采用标准库中的atoi函数,对于其他类型也都有相应的标准库函数,比如浮点型atof(),atol()等。
string str = "123";
int n = atoi(str.c_str());
cout << n;
关于atoi()函数,这个函数的主要功能是将一个字符串转换成一个数字,可能第一眼看上去,你会觉得这是一个很简单的函数,甚至是一个不需要多少行代码就可以实现的函数。其实这是一个看起来很简单,但是实践起来还有些需要注意的地方。总的来说,有以下5中情况:
1——指针为NULL
2——空字符处理
3——溢出处理
4——正号和负号的处理
5——如果遇到异常字符怎么处理
#include <iostream>
enum ret { kvalid = 0, kinvalid };//是否有非法输入的标记
int status = kvalid;
long long Strtointcode(const char* digit, bool minus)
{
long long num = 0;
while(*digit != '\0')
{
if(*digit >= '0' && *digit <= '9')
{
int flag = minus ? -1: 1;
num = num * 10 + flag*(*digit - '0');
if((!minus&&num > 0x7fffffff) || (minus&&num < (signed int)0x80000000))
{
num = 0; break;
}
digit++;
}
else
{
num = 0; break;
}
}
if(*digit == '\0')
{
status = kvalid;
}
return num;
}
int strtopoint(const char* str)
{
status = kinvalid;
long long num = 0;
if(str != NULL && *str != '\0')
{
bool minus = false;
if(*str == '+')
{
str++;
}
else if(*str == '-')
{
str++;
minus = true;
}
if(*str != '\0')
{
num = Strtointcode(str, minus);
}
}
return (int)num;
}
第二种方法是采用sstream头文件中定义的字符串流对象来实现转换
istringstream is("12");//构造输入字符串流,进行初始化
int i;
is >> i;//从is流中读入一个int整数存入i中
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下