C++ string应用总结

C++ string简单总结

前两小节不写代码和具体用法了。
后面比较复杂的写了代码,并且把输出内容注释上了。
代码可以直接运行。
欢迎指正。

访问和遍历

  1. at
  2. operator[]
  3. front
  4. back

String本身的大小和操作

  1. size
  2. max_size,capacity
  3. clear
  4. empty
  5. resize

C++ string的入门技巧(代码)

  1. operator+= 重载运算符+=

  2. append 添加 ,使用方法为a.append(b),其中a变化了。相当于a=a+b。
    append中可以添加参数 如a.append(b,0,2),把b的第0位开始的2个字符添加到a后面
    append中也可以添加一个string,如 a.append(“GoodBye JAVA.”),相当于在后面直接添加字符串;a.append(“GoodBye Python.”,6); 是拷贝前六位到a的结尾中

  3. push_back也是放在字符的最后,一般用于接收外部文本的信息。
    使用方式str.push_back(file.get());

  4. assign并不常用,是指定一个值。a.assign(b),类似于等于号“=”,但是assign更加的高效。
    也可以使用使用assign(b,0,2),把b的第0位开始的2个字符赋到a;使用assign(“string”)以及assign(“string”,长度),大致功能同上。

  5. insert,使用insert(索引,内容),在索引位置插入内容,后面的后移。
    使用insert(索引,内容,开始,结束),把内容的第几位到第几位插入到索引位置。
    使用insert(索引,指定内容,长度)类似,也可以直接使用临时变量 直接插入。

  6. replace,使用replace(索引,长度,字符串) ,从索引位置开始,把指定位置长度的字符替换成新的字符串

  7. swap太简单了不多说了

  8. erase(索引,长度 ),从第几位开始删除几位。

  9. pop_back(C++11新标准),类似于出栈,把最后一位弹掉了。

C++ string的高阶技巧(代码)

  1. atoi函数,将字符串转化为数字,需要借助c_str()。例如:atoi(num.c_str()),不然会报错。
    注:另一种通用方法为sstream。

  2. find函数,最最最重要的一个函数之一。find(字符,搜索开始位置(默认为0)),返回的是一个int类型的数字,告诉你在什么位置。例如:a.find(b)和a.find(b,found + 1),在a中搜索b。
    注:另有rfind函数(c++11),就是从后向前找不多说明。
    注:npos默认为-1.

  3. find_first_of
    find_last_of
    find_first_not_of
    find_last_not_of
    均为C++11新函数

  4. substr 比较实用 ,不会对本身造成影响,b = a.substr(6),只改变 b,不改变a,类似于java的切片,从第6位开始截取。
    如果使用b = a.substr(6,5),则为从第六位开始向后截取五位。

  5. compare,返回为0则相等,否则不相等。(见代码两种操作)

补充

  1. getline(只接收enter)
    如果使用cin>>name,直接输入的话不能让I love money全部进入name,只有I单独进入了。所以如果想把所有放入同一个字符串的话,就必须用getline。

代码及结果

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string a = "Hello World.";
    string b = "C++";
    string c = "JAVA";

    cout<<"使用+=(注意a没有变化):"<<endl;
    cout<< a + b <<endl;//Hello World.C++

  	cout<<"使用append(注意a变化了):"<<endl;
  	a.append(b);	
  	cout << a << endl;//Hello World.C++
  	//索引,长度
  	cout<<"使用append(b,0,2):"<<endl;
	a.append(b,0,2);
    cout << a <<endl;//Hello World.C++C+

    cout<<"使用append(一个string):"<<endl;
  	a.append("GoodBye JAVA.");	
  	cout << a << endl;//Hello World.C++C+GoodBye JAVA.

  	cout<<"使用append(一个string,长度):"<<endl;
  	a.append("GoodBye Python.",6);	
  	cout << a << endl;//Hello World.C++C+GoodBye JAVA.GoodBy

  	string str;
  	ifstream file("D:\\Devtools\\Sublime_workplace\\string_exercise.txt");
  	if(file)
  	{
  		while(!file.eof())
  			str.push_back(file.get());
  	}
  	cout << str << endl;

  	a = "Hello World";
  	cout<<"使用assign::"<<endl;//就是直接命名
  	a.assign(b);
  	cout << a << endl;//C++

  	cout<<"使用assign(b,0,2):"<<endl;
  	a.assign(b,0,2);//“索引为0开始,两个”
  	cout << a << endl;//C+

  	cout<<"使用assign(一个string):"<<endl;
  	a.assign("GoodBye JAVA.");//“索引为0开始,两个”
  	cout << a << endl; //GoodBye JAVA.


  	a = "Hello World.";
  	cout << "使用insert(索引,内容):"<<endl;
  	a.insert(0,"ABCDEFG");
  	cout <<a <<endl;	//ABACDEFGHello World.	

  	a = "Hello World";
  	cout << "使用insert(索引,内容,开始,结束):"<<endl;
  	a.insert(0,"ABCDEFG",3,4);
  	cout <<a <<endl;	//DEFGHello World.

  	a = "Hello World";
  	cout << "使用insert(位置,指定内容,长度):"<<endl;
  	a.insert(0,"QQ",3);
  	cout <<a <<endl;//QQ<0x00>Hello World


	a = "Hello World";
  	cout << "使用replace(索引,长度,字符串):"<<endl;
  	a.replace(3,2,"QQ");
  	cout <<a <<endl;//HelQQ World //0123
  	a.replace(3,2,"QQBF");
  	cout <<a <<endl;//HelQQBF World//会多出来

  	a = "Hello World.";
    b = "C++";
    a.swap(b);//比较简单不尝试了

    a = "Hello World.";
    a.erase(3,5);
    cout << "使用了erase(位置,长度(默认为1)消除:"<< a << endl;
    // Helrld.

    a = "Hello World.";
    a.pop_back();
    cout << a<<endl;//Hello World(.被删掉了)




    a = "Hello World.Hello C++.";
    b = "Hello";
    c = "small";

    //有关于将string转成int类型
    string num ="2020";

    int year = atoi(num.c_str());
    cout<<year<<endl;

    //还有一种sstream,比较通用方法

    int found = a.find(b);
    if(found != string::npos)	//npos默认为-1
    	cout << "first 'Hello' found at:"<<found<<endl;
    // 0

    found = a.find(b,found + 1);
    if(found != string::npos)
    	cout << "second 'Hello' found at:"<<found<<endl;
	// 13

    found = a.find(b,found + 1);
    cout << found <<endl;
    // -1


    a = "Hello World.Hello C++.";
    a.substr(6);
    cout<<"a仍为"<<a <<endl;
    //a仍为Hello World.Hello C++.
    b = a.substr(6);
    cout <<"b为(说明不改变原string)"<<b <<endl;
    //b为(说明不改变原string)World.Hello C++.
    
	b = a.substr(6,5);
    cout <<"substr(位置,长度):"<<b <<endl;
    //substr(位置,长度):World


    a = "C++ language";
    b = "JAVA language";
    cout << a.compare(b) <<endl;	
    //-1
    cout << a.compare(4,8,"language") <<endl;
    //0(0表示相等)

    string name;
    cout << "Please enter your name:"<<endl;
    getline(cin,name);
    cout << "Hello,"<< name <<endl;

    return 0;
} 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值