#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
//1:初始化
string str = "hello world";
string 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"
cout << "字符串13: " << str13 << endl;
//2:获取长度(length,size)
cout << "字符串的长度" << str1.length() << endl;
cout << "字符串的长度" << str1.size() << endl;
//3:插入(insert)
str = "hello world";
str2 = "hard ";
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"
//4:替换(replace)
str = "hello world";
str2 = "hard ";
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"
//5:添加(append)
str = "hello world";
str2 = "hard ";
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 "
//6:赋值(assign)
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); // str = "welcome"
//7:删除(erase)
str = "welcome to my blog";
//s.erase(pos,n) 把字符串s从pos开始的n个字符删除
str.erase(11,3); // str = "welcome to blog"
//8:剪切(substr)
str = "The apple thinks apple is delicious";
//s.substr(pos,n) 得到字符串s位置为pos后面的n个字符组成的串
string s1 = str.substr(4,5); // s1 = "apple"
//s.substr(pos) 得到字符串s从pos到结尾的串
string s2 = str.substr(17); // s2 = "apple is delicious"
//9:比较(compare)
/*两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。
若是遇到‘\0’结束比较,则长的子串大于短的子串,如:“9856” > “985”。
如果两个字符串相等,那么返回0,调用对象大于参数返回1,小于返回-1。*/
str1 = "small leaf";
str2 = "big leaf";
//s.compare(str) 比较当前字符串s和str的大小
cout << str1.compare(str2) << endl; // 1
//s.compare(pos,n,str) 比较当前字符串s从pos开始的n个字符与str的大小
cout << str1.compare(2,7,str2) << endl; // -1
//s.compare(pos,n0,str,pos2,n) 比较当前字符串s从pos开始的n0个字符与str中pos2开始的n个字符组成的字符串的大小
cout << str1.compare(6,4,str2,4,4) << endl; // 0
//s.compare(pos,n0,cstr,n) 比较当前字符串s从pos开始的n0个字符与字符数组cstr中前n个字符的大小
//此处不可将"big leaf"替换为str2
cout << str1.compare(6,4,"big leaf",4) << endl; // 1
//10:交换(swap)
str1 = "small leaf";
str2 = "big leaf";
//或者str1.swap(str2) ,输出结果相同
swap(str1,str2); // str1 = "big leaf" str2 = "small leaf"
swap(str1[0],str1[1]); // str1 = "ibg leaf"
//11:反转 (reverse)
str = "abcdefghijklmn";
reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"
//12:数值转化(sto*)
to_string(val); //把val转换成string
stoi(s,p,b); //把字符串s从p开始转换成b进制的int
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
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
//13:迭代器
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
//14:查找(find)
//14.1 find函数
str = "The apple thinks apple is delicious"; //长度34
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
//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函数
str = "The early birds catch the warm"; //长度30
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
return 0;
}
C++之string
于 2022-03-22 15:47:02 首次发布