文章目录
零、速查
1.声明一个C++字符串
声明一个字符串变量很简单:
string Str;
这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下:
a) string s; //生成一个空字符串s
b) string s(str) //拷贝构造函数 生成str的复制品
c) string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值
d) string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
e) string s(cstr) //将C字符串作为s的初值
f) string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。
g) string s(num,c) //生成一个字符串,包含num个c字符
h) string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值
i) s.~string() //销毁所有字符,释放内存
都很简单,我就不解释了。
2.字符串操作函数
这里是C++字符串的重点,我先把各种操作函数罗列出来,不喜欢把所有函数都看完的人可以在这里找自己喜欢的函数,再到后面看他的详细解释。
a) =,assign() //赋以新值
b) swap() //交换两个字符串的内容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase() //删除字符
f) clear() //删除全部字符
g) replace() //替换字符
h) + //串联字符串
i) ==,!=,<,<=,>,>=,compare() //比较字符串
j) size(),length() //返回字符数量
k) max_size() //返回字符的可能最大个数
l) empty() //判断字符串是否为空
m) capacity() //返回重新分配之前的字符容量
n) reserve() //保留一定量内存以容纳一定数量的字符
o) [ ], at() //存取单一字符
p) >>,getline() //从stream读取某值
q) << //将谋值写入stream
string和char*的转换
//参考:https://www.cnblogs.com/wuruofeng/p/10412559.html
r) copy() //将某值赋值为一个char[]
//例子:string s="hmmm";
//char p[50];
//s.copy(p, 5, 0);//这里5代表复制几个字符,0代表复制的位置,
//*(p+5)=‘\0’;//注意手动加结束符!!!
s) c_str() //将string内容以const char*返回
//例子:string s=“world”;
//const char *p = s.c_str();//同上,要加const
//或者 char* p=(char*)s.c_str();
//或者 char* p=const_cast<char *>(s.c_str());
t) data() //将内容以字符数组形式返回
u) substr() //返回某个子字符串
v)查找函数
w)begin() end() //提供类似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //返回配置
z)islower(char c) //是否为小写字母
isupper(char c) //是否为大写字母
isdigit(char c) //是否为数字
isalpha(char c)// 是否为字母
isalnum(char c)// 是否为字母或者数字
toupper(char c) //字母小转大
tolower(char c) //字母大转小
aa) //s为待分割字符串,delim为分割符,elems接受分割以后的字符串
void split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return;
}
一、初始化
初始化有两种方式,其中使用等号的是拷贝初始化,不使用等号的是直接初始化。
lstring str1 = "hello world"; // str1 = "hello world"
string str2("hello world"); // str2 = "hello world"
string str3 = str1; // str3 = "hello world"
string str4(str2); // str4 = "hello world"
string str5(10,'h'); // str5 = "hhhhhhhhhh"
string str6 = string(10,'h'); // str6 = "hhhhhhhhhh"
string str7(str1,6); // str7 = "world" 从字符串str1第6个字符开始到结束,拷贝到str7中
string str8 = string(str1,6); // str8 = "world"
string str9(str1,0,5); // str9 = "hello" 从字符串str1第0个字符开始,拷贝5个字符到str9中
string str10 = string(str1,0,5); // str10 = "hello"
char c[] = "hello world";
string str11(c,5); // str11 = "hello" 将字符数组c的前5个字符拷贝到str11中
string str12 = string(c,5); // str12 = "hello"
值得一提的是,如果:
string str13 = string("hello world",5) // str13 = "hello" 而非 " world"
1
此时,"hello world"应看作字符数组(参见str11),而非string对象(参见str7)
为了避免发生意外,在字符串插入、替换、添加、赋值、比较中去除了关于后一种的相关操作(参见后文)。
二、获取长度(length、size)
length()函数与size()函数均可获取字符串长度。
string str = "hello world";
cout << str.length() << str.size(); // 11 11
string::size_type len=str.length(); //字符串长度类型用string::size_type 更安全
string::size_type len=str.size();
当str.length()与其他类型比较时,建议先强制转换为该类型,否则会意想之外的错误。
比如:-1 > str.length() 返回 true。
三、插入(insert)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.insert(pos,n,ch) 在字符串s的pos位置上面插入n个字符ch
str.insert(6,4,'z'); // str = "hello zzzzworld"
//s.insert(pos,str) 在字符串s的pos位置插入字符串str
str.insert(6,str2); // str = "hello hard world"
//s.insert(pos,str,a,n) 在字符串s的pos位置插入字符串str中位置a到后面的n个字符
str.insert(6,str3,6,9); // str = "hello so happy world"
//s.insert(pos,cstr,n) 在字符串s的pos位置插入字符数组cstr从开始到后面的n个字符
//此处不可将"it is so happy wow"替换为str3
str.insert(6,"it is so happy wow",6); // str = "hello it is world"
四、替换(replace)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.replace(p0,n0,n,ch) 删除p0开始的n0个字符,然后在p0处插入n个字符ch
str.replace(0,6,4,'z'); // str = "zzzzworld"
//s.replace(p0,n0,str) 删除从p0开始的n0个字符,然后在p0处插入字符串str
str.replace(0,6,str2); // str = "hard world"
//s.replace(p0,n0,str,pos,n) 删除p0开始的n0个字符,然后在p0处插入字符串str中从pos开始的n个字符
str.replace(0,6,str3,6,9); // str = "so happy world"
//s.replace(p0,n0,cstr,n) 删除p0开始的n0个字符,然后在p0处插入字符数组cstr的前n个字符
//此处不可将"it is so happy wow"替换为str3
str.replace(0,6,"it is so happy wow",6); // str = "it is world"
五、添加(append)
append函数用在字符串的末尾添加字符和字符串。(同样与插入、替换对应理解)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.append(n,ch) 在当前字符串结尾添加n个字符c
str.append(4,'z'); // str = "hello worldzzzz"
//s.append(str) 把字符串str连接到当前字符串的结尾
str.append(str2); // str = "hello worldhard "
//s.append(str,pos,n) 把字符串str中从pos开始的n个字符连接到当前字符串的结尾
str.append(str3,6,9); // str = "hello worldso happy "
//append(cstr,int n) 把字符数组cstr的前n个字符连接到当前字符串结尾
//此处不可将"it is so happy wow"替换为str3
str.append("it is so happy wow",6); // str = "hello worldit is "
六、赋值(assign)
string str;
string temp = "welcome to my blog";
//s.assign(n,ch) 将n个ch字符赋值给字符串s
str.assign(10,'h'); // str = "hhhhhhhhhh"
//s.assign(str) 将字符串str赋值给字符串s
str.assign(temp); // str = "welcome to my blog"
//s.assign(str,pos,n) 将字符串str从pos开始的n个字符赋值给字符串s
str.assign(temp,3,7); // str = "come to"
//s.assaign(cstr,n) 将字符数组cstr的前n个字符赋值给字符串s
//此处不可将"it is so happy wow"替换为temp
str.assign("welcome to my blog",7); //welcome
七、删除(erase)
string str = "welcome to my blog";
//s.erase(pos,n) 把字符串s从pos开始的n个字符删除
str.erase(11,3); // str = "welcome to blog"
八、剪切(substr)
string str = "The apple thinks apple is delicious";
//s.substr(pos,n) 得到字符串s位置为pos后面的n个字符组成的串,包括pos位
string s1 = str.substr(4,5); // s1 = "apple"
//s.substr(pos) 得到字符串s从pos到结尾的串,包括pos位
string s2 = str.substr(17); // s2 = "apple is delicious"
九、比较(compare)
string str1 = "small leaf";
string str2 = "big leaf";
//s.compare(str) 比较当前字符串s和str的大小
cout << str1.compare(str2); // 1
//s.compare(pos,n,str) 比较当前字符串s从pos开始的n个字符与str的大小
cout << str1.compare(2,7,str2); // -1
//s.compare(pos,n0,str,pos2,n) 比较当前字符串s从pos开始的n0个字符与str中pos2开始的n个字符组成的字符串的大小
cout << str1.compare(6,4,str2,4,4); // 0
//s.compare(pos,n0,cstr,n) 比较当前字符串s从pos开始的n0个字符与字符数组cstr中前n个字符的大小
//此处不可将"big leaf"替换为str2
cout << str1.compare(6,4,"big leaf",4); // 1
十、交换(swap)
string str1 = "small leaf";
string str2 = "big leaf";
//或者str1.swap(str2) ,输出结果相同
swap(str1,str2); // str1 = "big leaf" str2 = "small leaf"
swap(str1[0],str1[1]); // str1 = "ibg leaf"
十一、反转(reverse)
string str = "abcdefghijklmn";
reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"
十二、数值转化(sto*)
to_string(val) //把val转换成string
stoi(s,p,b) //把字符串s从p开始转换成b进制的int
//atoi()的参数是const char *,因此对于字异字符串str我们必须调用c_str()的方法把这个string 转换成const char *类型的,
//stoi()的参数是const string *,不需要转换成const char *;
//stoi()会做范围检查,默认范围是在int的范围内的,如果超出范围的话则会runtime error
//stoi头文件:<string>,c++头文件
//atoi头文件:<cstdlib>,c++头文件 <stdlib.h>,c头文件
string s1 = "2147482", s2 = "-214748";
string s3 = "214748666666663", s4 = "-21474836488";
cout << stoi(s1) << endl;
cout << stoi(s2) << endl;
cout << atoi(s3.c_str()) << endl;
cout << atoi(s4.c_str()) << endl;
stol(s,p,b) //把字符串s从p开始转换成b进制的long
stoul(s,p,b) //把字符串s从p开始转换成b进制的unsigned long
stoll(s,p,b) //把字符串s从p开始转换成b进制的long long
例子:long long res = stoll(s);//把字符串s转换为long long
stoull(s,p,b) //把字符串s从p开始转换成b进制的unsigned long long
stof(s,p) //把字符串s从p开始转换成float
stod(s,p) //把字符串s从p开始转换成double
stold(s,p) //把字符串s从p开始转换成long double
字符串和十进制相互转换
(1)十进制转字符串
int a = 345;
string str = to_string(a);
cout <<str << endl;
输出:345
(2)字符串转十进制
string str = "3456";
int a = stoi(str);
cout << a << endl;
输出:3456
十进制和二进制相互转换
(1)十进制转二进制
int a = 10;
bitset<10> bit(a);
cout << bit << endl;
输出:0000001010
(2)二进制转十进制
第一种方法:
bitset<10> bit("010101");
int a = bit.to_ullong(); //这里为unsigned long long
cout << a << endl;
输出:21
第二种方法:
string out = "0101";
int x = stoi(out, nullptr, 2);
cout << x <<endl;
输出:5
字符串和二进制相互转换
(1)二进制转字符串
bitset<10> bit("010101");
string str = bit.to_string();
cout << str << endl;
输出:0000010101
(2)字符串转二进制
string str = "010101";
bitset<10> bit(str);
cout << bit << endl;
输出:0000010101
二进制、字符串、十六进制、十进制之间的转换
十三、迭代器(iterator)
string str = "abcdefghijklmn";
//s.begin() 返回字符串s第一个字符的位置
char a = *(str.begin()); // a
//s.end() 返回字符串s最后一个字符串的后一个位置
char b = *(str.end()-1); // n
//s.rbegin() 返回字符串s最后一个字符的位置
char c = *(str.rbegin()); // n
//s.rend() 返回字符串s第一个字符的前一个位置
char d = *(str.rend()-1); // a
十四、查找(find)
14.1 find函数
string str = "The apple thinks apple is delicious"; //长度34
string key = "apple";
//s.find(str) 查找字符串str在当前字符串s中第一次出现的位置
int pos1 = str.find(key); // 4
//s.find(str,pos) 查找字符串str在当前字符串s的[pos,end]中第一次出现的位置
int pos2 = str.find(key, 10); // 17
//s.find(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[pos,end]中第一次出现的位置
//此处不可将"delete"替换为str2(如果定义str2 = "delete")
int pos3 = str.find("delete", 0, 2); // 26
//s.find(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置
int pos4 = str.find('s', 0); // 15
//查找字符串strA是否包含子串strB,其中string:npos是个特殊值,说明查找没有匹配
strA.find(strB) != string::npos // strA包含子串strB
14.2 rfind函数
//s.rfind(str) 查找字符串str在当前字符串s中最后一次出现的位置
int pos5 = str.rfind(key); // 17
//s.rfind(str,pos) 查找字符串str在当前字符串s的[0,pos+str.length()-1]中最后一次出现的位置
int pos6 = str.rfind(key, 16); // 4
//s.rfind(cstr,pos,n) 查找字符数组cstr前n的字符在当前字符串s的[0,pos+n-1]中最后一次出现的位置
//此处不可将"apple"替换为key
int pos7 = str.rfind("apple", 40, 2); // 17
//s.rfind(ch.pos) 查找字符ch在当前字符串s的[0,pos]中最后一次出现的位置
int pos8 = str.rfind('s', 30); // 24
14.3 find_xxx_of函数
string str = "The early birds catch the warm"; //长度30
string key = "aeiou";
find_first_of
// s.find_first_of(str) 查找字符串str中的任意字符在当前字符串s中第一次出现的位置
int pos1 = str.find_first_of(key); // 2
//s.find_first_of(str,pos) 查找字符串str中的任意字符在当前字符串s的[pos,end]中第一次出现的位置
int pos2 = str.find_first_of(key, 10); // 11
//s.find_first_of(cstr,pos,n) 查找字符串str前n个任意字符在当前字符串s的[pos,end]中第一次出现的位置
//此处不可将"aeiou"替换为key
int pos3 = str.find_first_of("aeiou", 7, 2); // 17
//s.find_first_of(ch,pos) 查找字符ch在当前字符串s的[pos,end]中第一次出现的位置
int pos4 = str.find_first_of('r', 0); // 6
find_first_not_of
//s.find_first_not_of(str) 查找字符串str之外的任意字符在当前字符串s中第一次出现的位置
int pos1 = str.find_first_not_of(key); // 0
//s.find_first_not_of(str,pos) 查找字符串str之外的任意字符在当前字符串s的[pos,end]中第一次出现的位置
int pos2 = str.find_first_not_of(key, 10); // 10
//s.find_first_not_of(cstr,pos,n) 查找字符串str前n个之外任意字符在当前字符串s的[pos,end]中第一次出现的位置
//此处不可将"aeiou"替换为key
int pos3 = str.find_first_not_of("aeiou", 7, 2); // 7
//s.find_first_not_of(str) 查找字符ch之外任意字符在当前字符串s的[pos,end]中第一次出现的位置
int pos4 = str.find_first_not_of('r', 0); // 0
find_last_of
//s.find_last_of(str) 查找字符串str中的任意字符在当前字符串s中最后一次出现的位置
int pos1 = str.find_last_of(key); // 27
//s.find_last_of(str,pos) 查找字符串str中的任意字符在当前字符串s的[0,pos]中最后一次出现的位置
int pos2 = str.find_last_of(key, 15); // 11
//s.find_last_of(cstr,pos,n) 查找字符串str前n个任意字符在当前字符串s的[0,pos]中最后一次出现的位置
//此处不可将"aeiou"替换为key
int pos3 = str.find_last_of("aeiou", 20, 2); // 17
//s.find_last_of(str) 查找字符ch在当前字符串s的[0,pos]中最后一次出现的位置
int pos4 = str.find_last_of('r', 30); // 28
find_last_not_of
//s.find_last_not_of(str) 查找字符串str之外的任意字符在当前字符串s中最后一次出现的位置
int pos1 = str.find_last_not_of(key); // 29
//s.find_last_not_of(str,pos) 查找字符串str之外的任意字符在当前字符串s的[0,pos]中最后一次出现的位置
int pos2 = str.find_last_not_of(key, 15); // 15
//s.find_last_not_of(cstr,pos,n) 查找字符串str前n个之外任意字符在当前字符串s的[0,pos]中最后一次出现的位置
//此处不可将"aeiou"替换为key
int pos3 = str.find_last_not_of("aeiou", 20, 2); // 20
//s.find_last_not_of(str) 查找字符ch之外任意字符在当前字符串s的[0,pos]中最后一次出现的位置
int pos4 = str.find_last_not_of('r', 30); // 29
十五、自己实现string类
#include <algorithm>
#include <iostream>
#include <fstream>
#pragma warning(disable:4996)
using namespace std;
fstream fout("String.txt", ios::app);
class String
{
public:
String(const char* str = NULL);//普通的构造函数
String(const String& other); //拷贝构造函数
~String();
String& operator = (const String& other);//复制构造
private:
char* m_data;//用于保存字符串
};
String::String(const char* str)
{
if (str == NULL)
{
m_data = new char[1];
*m_data = '\0';
fout << "String::String(const char* str=NULL)" << endl;
}
else
{
int len = strlen(str);
m_data = new char[len + 1];
strcpy(m_data, str);
fout << "String::String(const char* str)" << endl;
}
}
String::~String()
{
if (m_data != NULL)
{
delete[] m_data;
m_data = NULL;
fout<<"String::~String()"<<endl;
}
}
String::String(const String & other)
{
int len = strlen(other.m_data);
m_data = new char[len + 1];
strcpy(m_data, other.m_data);
fout<<"String::String(const String & other)"<<endl;
}
String& String::operator = (const String & other)
{
if (this == &other)
{
cout << "String& String::operator = (const String & other),&other==this" << endl;
return *this;
}
delete[] m_data;
int len = strlen(other.m_data);
m_data = new char[len + 1];
strcpy(m_data ,other.m_data);
fout << "String& String::operator = (const String & other)" << endl;
return *this;
}
int main() {
const char* temp1 = "qwertyuiop";
String test1;//String::String(const char* str=NULL)
String test2(temp1);//String::String(const char* str)
String test3=test2;//String::String(const String & other)
String test4(test2);//String::String(const String & other)
test4 = test2;//String& String::operator = (const String & other)
const char* temp2 = "asdfghjkl";
String test5(temp2);//String::String(const char* str)
test5 = test4;//String& String::operator = (const String & other)
system("pause");
return 0;
}
完整输出: