C++ string简单总结
前两小节不写代码和具体用法了。
后面比较复杂的写了代码,并且把输出内容注释上了。
代码可以直接运行。
欢迎指正。
访问和遍历
- at
- operator[]
- front
- back
String本身的大小和操作
- size
- max_size,capacity
- clear
- empty
- resize
C++ string的入门技巧(代码)
-
operator+= 重载运算符+=
-
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的结尾中 -
push_back也是放在字符的最后,一般用于接收外部文本的信息。
使用方式str.push_back(file.get()); -
assign并不常用,是指定一个值。a.assign(b),类似于等于号“=”,但是assign更加的高效。
也可以使用使用assign(b,0,2),把b的第0位开始的2个字符赋到a;使用assign(“string”)以及assign(“string”,长度),大致功能同上。 -
insert,使用insert(索引,内容),在索引位置插入内容,后面的后移。
使用insert(索引,内容,开始,结束),把内容的第几位到第几位插入到索引位置。
使用insert(索引,指定内容,长度)类似,也可以直接使用临时变量 直接插入。 -
replace,使用replace(索引,长度,字符串) ,从索引位置开始,把指定位置长度的字符替换成新的字符串
-
swap太简单了不多说了
-
erase(索引,长度 ),从第几位开始删除几位。
-
pop_back(C++11新标准),类似于出栈,把最后一位弹掉了。
C++ string的高阶技巧(代码)
-
atoi函数,将字符串转化为数字,需要借助c_str()。例如:atoi(num.c_str()),不然会报错。
注:另一种通用方法为sstream。 -
find函数,最最最重要的一个函数之一。find(字符,搜索开始位置(默认为0)),返回的是一个int类型的数字,告诉你在什么位置。例如:a.find(b)和a.find(b,found + 1),在a中搜索b。
注:另有rfind函数(c++11),就是从后向前找不多说明。
注:npos默认为-1. -
find_first_of
find_last_of
find_first_not_of
find_last_not_of
均为C++11新函数 -
substr 比较实用 ,不会对本身造成影响,b = a.substr(6),只改变 b,不改变a,类似于java的切片,从第6位开始截取。
如果使用b = a.substr(6,5),则为从第六位开始向后截取五位。 -
compare,返回为0则相等,否则不相等。(见代码两种操作)
补充
- 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;
}