string s (str) :生成字符串s为str的复制品
string s(str, str_begin, str_len):将字符串str中从下标str_begin开始、长度为strlen的部分作为字符串s的初值。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("0123456789");
string b(a,0,5);
string c(a,1,8);
cout<<a<<endl<<b<<endl<<c<<endl;
return 0;
}
/*
0123456789
01234
12345678
Process returned 0 (0x0) execution time : 0.120 s
Press any key to continue.*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("0123456789");
cout<<a.length()<<endl;
cout<<a.size()<<endl;
cout<<a.max_size()<<endl;
return 0;
}
/*
10
10
2147483647
Process returned 0 (0x0) execution time : 0.112 s
Press any key to continue.*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("0123456789");
string b("01245");
string c("acv");
cout<<a.compare(c)<<endl;
cout<<b.compare(c)<<endl;
cout<<a.compare(b)<<endl;
if(a<b)
cout<<"a<b"<<endl;
return 0;
}
//-1
-1
-1
a<b
Process returned 0 (0x0) execution time : 0.069 s
Press any key to continue.
// 尾插一个字符
s1.push_back(‘a‘);
s1.push_back(‘b‘);
s1.push_back(‘c‘);
cout<<"s1:"<<s1<<endl; // s1:abc
// insert(pos,char):在制定的位置pos前插入字符char
s1.insert(s1.begin(),‘1‘);
cout<<"s1:"<<s1<<endl; // s1:1abc
//方法一:append()
string s1("abc");
s1.append("def");
cout<<"s1:"<<s1<<endl; // s1:abcdef
// 方法二:+ 操作符
string s2 = "abc";
/*s2 += "def";*/
string s3 = "def";
s2 += s3.c_str();
cout<<"s2:"<<s2<<endl; // s2:abcdef
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("abcdef");
// 下标
for(int i=0;i<s1.length();i++)
cout<<s1[i];
cout<<endl;
// 正向迭代器
string::iterator iter = s1.begin();
for( ; iter < s1.end() ; iter++)
{
cout<<*iter;
}
cout<<endl;
// 反向迭代器
string::reverse_iterator riter = s1.rbegin();
for( ; riter < s1.rend() ; riter++)
{
cout<<*riter;
}
cout<<endl;
return 0;
}
//
abcdef
abcdef
fedcba
Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("abcdef");
s1.erase(s1.begin()+1);
cout<<s1<<endl;
string s2("I Love China");
s2.erase(s2.begin()+1,s2.end()-1);
cout<<s2<<endl;
string s3("I Love China");
s3.erase(1,7);
cout<<s3<<endl;
return 0;
}
//
> cd "f:\Yqifei_javacode\" ; if ($?) { g++ qi.cpp -o qi } ; if ($?) { .\qi }
acdef
Ia
Ihina
PS F:\Yqifei_javacode>
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("hello,world!");
string s2("hello,world!");
string s3("hello,world!");
s1.replace(6,6,"girl");
s2.replace(0,12,"boy,girl");
cout<<s1<<endl;
cout<<s2<<endl;
return 0;
}
//
hello,girl
boy,girl
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1("hello,world!");
for(int i=0;i<s1.size();i++){
s1[i]=toupper(s1[i]);
}
cout<<s1<<endl;
string s2("hello,world!"),s3;
transform(s2.begin(),s2.end(),s2.begin(),::toupper);
cout<<s2<<endl;
return 0;
}
//
HELLO,WORLD!
HELLO,WORLD!
string s("dog bird chicken bird cat");
//字符串查找-----找到后返回首字母在字符串中的下标
// 1. 查找一个字符串
cout << s.find("chicken") << endl; // 结果是:9
// 2. 从下标为6开始找字符‘i‘,返回找到的第一个i的下标
cout << s.find(‘i‘, 6) << endl; // 结果是:11
sort(iterator iter1, iterator iter2):对[iter1, iter2)进行排序
ps:for(auto x:a) cout<<x<<" "这是基于范围的for循环,只有C++11版本以上才支持。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a("sdfergrggqeiufn");
sort(a.begin(),a.end());
for(auto x:a)
cout<<x<<" ";
cout<<endl;
return 0;
}
//
d e e f f g g g i n q r r s u
Process returned 0 (0x0) execution time : 0.020 s
Press any key to continue.
参数:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "12345";
string s1 = s.substr(3);
string s2 = s.substr(2, 3);
cout << s1 << ‘ ‘ << s2;
return 0;
}
//
45 345
Process returned 0 (0x0) execution time : 0.020 s
Press any key to continue.
原文:https://www.cnblogs.com/Yqifei/p/12660178.html