c++常用

C++中map用法详解

string类中的方法:

to_string

std::string to_string( int value ); (1) (C ++11起) 
std::string to_string( long value ); (2) (C ++11起) 
std::string to_string( long long value ); (3) (C ++11起) 
std::string to_string( unsigned value ); (4) (C ++11起) 
std::string to_string( unsigned long value ); (5) (C ++11起) 
std::string to_string( unsigned long long value ); (6) (C ++11起) 
std::string to_string( float value ); (7) (C ++11起) 
std::string to_string( double value ); (8) (C ++11起) 
std::string to_string( long double value ); (9) (C ++11起)

find

size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;

参数说明:pos查找起始位置    n待查找字符串的前n个字符

使用样例:

string  str1("the usage of find can you use it");

string  str2("the");

上面定义出了两个字符串;

str1.find(str2);                    //  从串str1中查找时str2,返回str2中首个字符在str1中的地址

str1.find(str2,5);                //   从str1的第5个字符开始查找str2

str1.find("usage");            //   如果usage在str1中查找到,返回u在str1中的位置

str1.find("o");                     //   查找字符o并返回地址

str1.find("of big",2,2);      //   从str1中的第二个字符开始查找of big的前两个字符

 

find_first_of

函数原型:

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;

参数和find基本相同,不在赘述!

特别注意:

find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;

比如:

string str1("I am change");

string  str2("about");

int k=str1.find_first_of(str2);    //k返回的值是about这5个字符中任何一个首次在str1中出现的位置;

 int find_first_of(char c, int start = 0):

              查找字符串中第1个出现的c,由位置start开始。
              如果有匹配,则返回匹配位置;否则,返回-1.默认情况下,start为0,函数搜索
              整个字符串。
              
int find_last_of(char c):
              查找字符串中最后一个出现的c。有匹配,则返回匹配位置;否则返回-1.
              该搜索在字符末尾查找匹配,所以没有提供起始位置。

 string::npos的用法

npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::container_type::size_type。

1、npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。举例如下(计算字符串中含有的不同字符的个数)。

2、string::npos作为string的成员函数的一个长度参数时,表示“直到字符串结束(until the end of the string)”。例如:
  1. tmpname.replace(idx+1, string::npos, suffix);  
这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。
 
 
stoi(string s);把字符串转换为int型数字
isdigit(char ch);判断字符ch是否是数字
 

1.1 std::string::operator+=

  • 原型:string& operator+= (const string& str);
  • 说明:把string型字符串str连接到源字符串的结尾。
  • 原型:string& operator+= (const char* s);
  • 说明:把char型字符串s连接到源字符串的结尾。
  • 原型:string& operator+= (char c);
  • 说明:把字符c连接到源字符串的结尾。

1.2 std::string::append

  • 原型:string& append (const string& str);
  • 说明:把string型字符串str连接到源字符串结尾。
  • 原型:string& append (const string& str, size_t subpos, size_t sublen = npos);
  • 说明:用string型字符串str中从下标为subpos处开始的sublen长度的字符串连接到当前字符串结尾。
  • 原型:string& append (const char* s);
  • 说明:把char型字符串s连接到当前字符串结尾。
  • 原型:string& append (const char* s, size_t n);
  • 说明:用char型字符串s开始的n个字符连接到当前字符串结尾。
  • 原型:string& append(size_t n, char c);
  • 说明:在当前字符串结尾添加n个字符c。
  • 原型:template < class InputIterator> string& append(InputIterator first, InputIterator last);
  • 说明: Appends a copy of the sequence of characters in the range [first,last), in the same order.
  • 原型:string& append(initializer_list il);
  • 说明: Appends a copy of each of the characters in il, in the same order.

1.3 std::string::push_back

  • 原型:void push_back (char c);
  • 说明:添加字符到字符串的末尾。
posted @ 2018-03-15 22:09  -桃之夭夭  阅读(182)  评论(0编辑  收藏  举报