c++字符串学习笔记

字符串(string 类)

声明

#include<string>
#include<iostream>
using namespace std;

string str1;//生成空字符串
cin << str1;
cout << str1;

string str2("hello");//生成并初始化
cout << str2;

string str3(str2);//hello
cout << str3;

string str4(10,'H');//HHHHHHHHHH
cout << str4;

char a[] = "hello";//hello
string str5(a);
cout << str5;

连接(追加):append()函数

string str("Hello");
char sz[] = "Word";
string str6("Word");

str.append(10,'W');//追加单个字符

str.append(sz);//追加c风格字符串

str.append(str6);//追加字符串

比较:compare()函数

返回值:0、1、-1

前 > 后 返回1、前 < 后 返回-1、前 = 后 返回0;

string date1("21");
string date2("22");
string date3("22");
char date4[] = "23";

cout << date1.compare(date2) << endl;//-1
cout << date2.compare(date1) << endl;//1
cout << date2.compare(date3) << endl;//0
cout << date3.compare(date4) << endl;//-1

查找:find()函数

string findStr("ABCDEF");
cout << findStr.find('D', 2) << findStr.find('D', 5);
//第一个参数是要查找的字符,第二个参数是起始查找的位置。返回值为该字符在字符串中的位置int(0位起),若无则返回-1.

char findChar = "BCD";
cout << findStr.find(findChar, 0);
//返回字串在string中的位置,本题为1

string findStr1("ABC");
cout << findStr.find(findStr1, 0);

替换:replace()函数

string replaceStr("ABCDEFG");
cout << replaceStr.replace(2, 5, "2345");//先删除C-E,在插入2345. AB2345FG。
cout << replaceStr.replace(2, 5, 10, 'M');//先删除,再插入10个M

插入:incert()函数

string incertStr("123456");
string incertStr1("ABCDEF");
cout << incertStr.incert(3, incertStr1);

删除 & 提取子串:erase() & substr()函数

string str_test("ABCDEFG");
str_test.erase(3, 3); //从D起 删除3个字符

str_test.substr(3, 3);//返回string, 提取DEF。

获取字符串长度 str.length()

image-20210815211032501

image-20210817154349696

字符串流(常用于类型转换)

注意

  1. 再进行多次转换的时候,必须调用stringstream的成员函数clear().
  2. clear()重置流的标志状态;str()清空流的内存缓冲,重复使用内存消耗不再增加!
  3. 在多次数据类型转换的场景下,必须使用 clear() 方法清空 stringstream,不使用 clear()方法或使用 str("") 方法,都不能得到数据类型转换的正确结果。
#include<sstream> 
stringstream stream;
stream.clear();
stream.str("");
用法1 数据类型的转换
// int -> string
stringstream sstream;
string strResult;
int nValue = 1000;
 
// 将int类型的值放入输入流中
sstream << nValue;
// 从sstream中抽取前面插入的int类型的值,赋给string类型
sstream >> strResult;
用法2 多字符串的拼接
stringstream sstream;
// 将多个字符串放入 sstream 中
sstream << "first" << " " << "string,";
sstream << " second string";
// 可以使用 str() 方法,将 stringstream 类型转换为 string 类型;
cout << "strResult is: " << sstream.str() << endl;
用法3 可以用于分割被空格、制表符等符号分割的字符串
#include<iostream>  
#include<sstream>        //istringstream 必须包含这个头文件
#include<string>  
using namespace std;  
int main(){  
    string str="i am a boy";  
    istringstream is(str);  
    string s;  
    while(is>>s)  {  
        cout<<s<<endl;  
    }  
} 
用法4 实现任意类型的转换
 template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
    }

int main(){
    string s = "1 23 # 4";
    stringstream ss;
    ss<<s;
    while(ss>>s){
        cout<<s<<endl;
        int val = convert<int>(s);
        cout<<val<<endl;
    }
    return 0;
}
posted @ 2021-10-09 09:10  wzx1210  阅读(63)  评论(0)    收藏  举报