string
其实不完全算STL库中的。在 C++ 中,string 是标准库提供的用于处理动态字符序列的类(位于 头文件),相比 C 风格的字符数组(char[] 或 char*),string 提供更安全、更便捷的操作。
#include
string name;//创建一个空的string
string name("数据");//创建一个字符串值为“数据”的string
string name="数据";//创建一个字符串值为“数据”的string
string name(n , 'C')//创建一个有n个字符‘C’的string
string name(other_string);//把other_string 的值拷贝到当前string
string name( other_string , pos , n_size_char );//另一个string的索引为pos到pos+n的值都拷贝到name中
string name{"初始化列表"};//name用初始化列表来初始化
string name={"初始化列表"};//name用初始化列表来初始化
#include
using namespace std; // 或直接使用 std::string
string s1; // 空字符串 ""
string s2("Hello"); // 从 C 字符串初始化:s2 = "Hello"
string s3 = "World"; // 等价于 s3("World")
string s4(5, 'a'); // 重复字符:s4 = "aaaaa"
string s5(s2); // 拷贝构造:s5 = "Hello"
string s6(s3, 1, 3); // 子串:从索引1取3字符 → s6 = "orl"
string s7{"1,2,3"}; //s7值为“1,2,3”
string s8={"faj"}; //s8值为"faj"
首先这里使用的头文件
#include
#include
using namespace std;
int main(int argv,char* argc[]){
string s1("hello");
string::iterator it =s1.begin();
cout<<*it<<endl;//h
}
int main(int argv,char* argc[]){
string s1("hello");
string::iterator it = s1.end();
cout<<*(--it);//o
//--的意思就是把end()指针向后移一位,移到了尾部数据o的位置
}
int main(int argv,char* argc[]){
string s1("hello");
char num = s1[0];
cout<<s1[0]<<endl;//h
cout<<num<<endl;//h
}
int main(int argv,char* argc[]){
string s1("hello");
char num = s1.at(1);
cout<<s1.at(1)<<endl; //e
cout<<num<<endl; //e
}
int main(int argv,char* argc[]){
string s1("hello");
char num = s1.front();
cout<<s1.front()<<endl;//h
cout<<num<<endl;//h
}
int main(int argv,char* argc[]){
string s1("hello");
char num = s1.back();
cout<<s1.back()<<endl;//o
cout<<num<<endl;//o
}
int main(int argv,char* argc[]){
string s1("hello world");
cout<<s1.data()<<endl;//hello world
}
int main(int argv,char* argc[]){
string s1("hello world");
string s2("read");
s1+=s2;
cout<<s1<<endl;//hello worldread
s1+="123";
cout<<s1<<endl;//hello worldread123
}
int main(int argv,char* argc[]){
string s1("hello world");
char c = '2';
s1.push_back('1');
cout<<s1<<endl;//hello world1
s1.push_back(c);
cout<<s1<<endl;//hello world12
}
int main(int argv,char* argc[]){
string s1("hello world");
string s2("read");
s1.append(s2);
cout<<s1<<endl;//hello worldread
s1.append("123");
cout<<s1<<endl;//hello worldread123
}
int main(int argv,char* argc[]){
string s1("hello");
string s2(" world");
s1.insert(s1.end(),s2.begin(),s2.end());
cout<<s1<<endl;//hello world
}
int main(int argv,char* argc[]){
string s1("hello");
s1.insert(s1.end(),'1');
cout<<s1<<endl;//hello1
}
int main(int argv,char* argc[]){
string s1("hello");
s1.insert(0,10,'1');
cout<<s1<<endl;//hello1111111111
}
int main(int argv,char* argc[]){
string s1("hello");
s1.insert(0,{"123"});
cout<<s1<<endl;//123hello
}
int main(int argv,char* argc[]){
string s1("hello");
s1.pop_back();
cout<<s1<<endl;//hell
}
int main(int argv,char* argc[]){
string s1("hello");
s1.erase(1);
cout<<s1<<endl;//h
}
int main(int argv,char* argc[]){
string s1("hello");
s1.erase(++s1.begin());
cout<<s1<<endl;//hllo
}
int main(int argv,char* argc[]){
string s1("hello");
s1.erase(s1.begin(),s1.end());
cout<<s1<<endl;//
}
int main(int argv,char* argc[]){
string s1("hello");
s1.erase(0,2);
cout<<s1<<endl;//llo
}
int main(int argv,char* argc[]){
string s1("hello");
string s2("world");
if(s1<s2){
cout<<"true";
}else{
cout<<"false";
}
//true
}
int main(int argv,char* argc[]){
string s1("hello");
string s2("world");
if(s1.compare(s2)){
cout<<"true";
}else{
cout<<"false";
}
//true
}
int main(int argv,char* argc[]){
string s1("hello");
string s2("world");
if(s1.compare(0,1,s2)){//h
cout<<"true";
}else{
cout<<"false";
}
//true
}
int main(int argv,char* argc[]){
string s1("hello");
string s2("world");
if(s1.compare(0,1,s2,0,1)){//h w
cout<<"true";
}else{
cout<<"false";
}
//true
}
int main(int argv,char* argc[]){
string s1("hello");
string s2("world");
int num1 = s1.size();
int num2 = s2.length();
cout<<num1<<endl;//5
cout<<num2<<endl;//5
}
int main(int argv,char* argc[]){
string s1("hello");
s1.reserve(10);
int num1 = s1.size();
cout<<num1<<endl;//5
}
int main(int argv,char* argc[]){
string s1("hello");
s1.reserve(10);
int num1 = s1.size();
cout<<num1<<endl;//5
s1.shrink_to_fit();
}
int main(int argv,char* argc[]){
string s1("hello");
string s2 =s1.substr(0,1);
cout<<s1<<endl;//hello
cout<<s2<<endl;//h
}
size_t pos = s2.find("ll"); // 返回首次出现的位置 → 2
if (pos != string::npos) { // npos表示未找到(值为-1)
cout << "Found at " << pos;
}
pos = s2.rfind("l"); // 反向查找 → 3
string text = "C++ is powerful. C++ is fast.";
// 查找 "C++" 的第一次出现
size_t first = text.find("C++"); // first = 0
// 从索引 5 开始查找 "C++" 的下一次出现
size_t next = text.find("C++", first + 1); // next = 14
int main(int argv,char* argc[]){
string s1("hello");
string s2 =s1.substr(0,1);
s1.replace(1,2,"1");
cout<<s1<<endl;//h1lo
}
int main(int argv,char* argc[]){
string s1("hello");
string s2 =s1.substr(0,1);
s1.swap(s2);
cout<<s1<<endl;//h
}
int main(int argv,char* argc[]){
string string1={"423 42"};
string string2;
cout<<string1<<endl;//423 42
getline(cin,string2);//fsd sdf
cout<<string2<<endl;//fsd sdf
}