c++中数字与string转换

网上流传的其他什么sstream,sprintf过于麻烦,提供便于记忆版本。

 

1.int->string

 

//头文件

//#include

//注意这个是string,没有c

//从C++11开始引入

 

std::to_string(int)

std::to_string(long)

std::to_string(long long)

std::to_string(float)

std::to_string(double)

std::to_string(long double)

还支持各类unsigned,基本上主流数值类型都能无脑转换

 

//转换时保留负号

//参考https://blog.csdn.net/lzuacm/article/details/52704931

//参考https://blog.csdn.net/u010510020/article/details/73799996

//http://www.cplusplus.com/reference/string/to_string/

 

2. string->int

 

//头文件

//#include

//标准库函数,但是使用时一般会配合

 

std::stoi

std::stol

std::stoll

//看名字就知道对应为int,long,long long

 

int stoi (const string&  str, size_t* idx = 0, int base = 10)

//idx是一个指针,该指针指向一个size_t类型的对象
//传入指针地址后,该对象的值会被修改为string中数值后的第一个字符所在位置
//例如stoi("123abcd",&p),返回的p指向a所在
//也可以把p的位置理解为数值部分的结束位置
//int base是进制基数,默认10进制没什么好说的
//需要注意的是stoi实质上是调用stol的

 

 

//值得一提的是,在使用过程中发现,stoi会自动将string数值部分的首位0消去

//同时还会保留负号,简直贴心,好评如潮

//例如sto("00123abc"),结果为123,自动消去首位无效0

//stoi("-0123"),结果为-123。 

 

//参考http://www.cplusplus.com/reference/string/stoi/

//参考http://www.cplusplus.com/reference/string/stol/

 

 

3.截取替换查找

 

下面的pos就是size_t类型的位置。

记得上面stoi(string, &p )吗

p是size_t类型的,其值会被修改数值后第一个字符所在位置(第一个字符为0)

亦即数值部分的结束位置。

c++中数字与string转换_第1张图片

 

例如:

size_t p;

stoi("123abc",&p);

str2 = s.substr(p); // 从p位置开始,输出"abc"

 

1. 截取子串

       s.substr(pos, n)    截取s中从pos开始(包括0)的n个字符的子串,并返回

       s.substr(pos)        截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回



2. 替换子串

       s.replace(pos, n, s1)    用s1替换s中从pos开始(包括0)的n个字符的子串



3. 查找子串

       s.find(s1)         查找s中第一次出现s1的位置,并返回(包括0)

       s.rfind(s1)        查找s中最后次出现s1的位置,并返回(包括0)

       s.find_first_of(s1)       查找在s1中任意一个字符在s中第一次出现的位置,并返回(包括0)

       s.find_last_of(s1)       查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)

       s.find_first_not_of(s1)         查找s中第一个不属于s1中的字符的位置,并返回(包括0)

       s.find_last_not_of(s1)         查找s中最后一个不属于s1中的字符的位置,并返回(包括0)



//参考https://blog.csdn.net/ezhou_liukai/article/details/13779091

 

 

4.string查找的一些说明

 

c++定义的重载find()有4种:

string (1)	
size_t find (const string& str, size_t pos = 0) const;
c-string (2)	
size_t find (const char* s, size_t pos = 0) const;
buffer (3)	
size_t find (const char* s, size_t pos, size_t n) const;
character (4)	
size_t find (char c, size_t pos = 0) const;

 

1.从str1的第p个位置开始查找str2

2.对c中char*形式字符串数组的兼容形式,效果同1

3.从str1的第pos个位置开始,查找str2的前n个字符,其中str2是char*类型

4.对单个字符char c的兼容形式,效果同1

 

 

特别的:

//s.find()查找不到时,返回一个特别的静态常量,在c++中被定义为std::string::npos;

//在vs2013中测试得到npos=4294967295,该值为unsigned int的理论最大值,转化为有符号即-1;

 

5.string替换的重载

//string (1)	
string& replace (size_t pos,  size_t len,  const string& str);
string& replace (iterator i1, iterator i2, const string& str);

//substring (2)	
string& replace (size_t pos,  size_t len,  const string& str,
                 size_t subpos, size_t sublen);

//c-string (3)	
string& replace (size_t pos,  size_t len,  const char* s);
string& replace (iterator i1, iterator i2, const char* s);

//buffer (4)	
string& replace (size_t pos,  size_t len,  const char* s, size_t n);
string& replace (iterator i1, iterator i2, const char* s, size_t n);

//fill (5)	
string& replace (size_t pos,  size_t len,  size_t n, char c);
string& replace (iterator i1, iterator i2, size_t n, char c);

//range (6)	
template 
  string& replace (iterator i1, iterator i2,
                   InputIterator first, InputIterator last);

 

//fill(5),意思是从pos开始的len长度的子串,被替换为n个字符c。跟insert一样,当对象为char时,需要额外指定char的重复次数。 

 

//参考http://www.cplusplus.com/reference/string/string/npos/

//参考http://www.cplusplus.com/reference/string/string/find/

//参考https://www.cnblogs.com/web100/archive/2012/12/02/cpp-string-find-npos.html

 

 

 

 

4.string 转 char[]的说明

不行开新文章了,直接更新在这。

 

4.1 string内置s.c_str(),返回一个const char*指针

string s="hello";

const char* p = s.c_str();

这是个临时指针,指向常量区某位置。修改s内容后,该指针不保证继续生效。

 

4.2 string 转 char[]

 

基本方法,逐位赋值。

可以用上面的.c_str(),拿到指针p后,逐位访问。不过string类本身也支持逐位访问。

这种方法显得比较蠢,而且还需要手动补结束符号。

string s = "hello";
char a[10];
int i;
for(i=0;i

 

进阶方法,内置函数sscanf,一行语句搞定。

注意,sscanf只能从const char* 中读取内容,所以需要用到上文说的.c_str()函数。

string s="hello";
char a[6];
sscanf(s.c_str(), "%s", a);

特别注意:char[]数组的长度需要预留一位给'\0',总是犯这个错误。

 

 

5.string删除 - erase()

隔一段时间不写oj,这些基础api又会忘记。

不开新帖了,更在这里以后要刷题了就回来看看。

basic_string & erase(size_type pos=0, size_type n=npos); / /从指定pos开始erase指定n个字符

iterator erase(const_iterator position) //删除指定迭代器所指字符, 并返回下个字符的迭代器

iterator erase(const_iterator first, const_iterator last) //删除[first, last)区间内所有字符

后面两个用的少,主要是第一个erase(pos,n)。

 

6.string插入insert

 

string &insert(int p0, const char *s);——在p0位置插入字符串s

string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n个字符

string &insert(int p0,const string &s);——在p0位置插入字符串s

string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符

string &insert(int p0, int n, char c);//在p0处插入n个字符c。先n后c。

iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符

void insert(iterator it, int n, char c);//在it处插入n个字符c

 

需求实例:

我有一串"aabbb",想在末尾加入3个'c'。

坑爹的是c里面字符串没有末尾插入的pushback这种东西。

用insert还得先get length找到end所在position。

这时候多么怀念python啊。。。。strt+3*'c'就能搞定了。 

你可能感兴趣的:(c++)