string类型 C++

(C++递归预习到了string类型,这个是处理字符串的一个非常好用的东西,在C里面没有.今天来学习一下)

顺便推荐一个很不错的网站:http://c.biancheng.net/view/400.html

 



首先,为了在程序中使用string类型,必须包含头文件 <string>。如下:

   #include <string>

(注意这里不是string.h,string.h是C字符串头文件。)

string类是一个模板类,位于名字空间std中,通常为方便使用还需要增加:

   using namespace std;

声明一个字符串变量很简单:

string str;

 

 

一.对象赋值:

 1  string str;  //定义了一个空字符串str
 2 
 3     str = "Hello world";   // 给str赋值为"Hello world"
 4 
 5     char cstr[] = "abcde";  //定义了一个C字符串
 6 
 7     string s1(str);       //调用复制构造函数生成s1,s1为str的复制品
 8 
 9     cout<<s1<<endl;
10 
11     string s2(str,6);     //将str内,开始于位置6的部分当作s2的初值
12 
13     cout<<s2<<endl;
14 
15     string s3(str,6,3);  //将str内,开始于6且长度顶多为3的部分作为s3的初值
16 
17         cout<<s3<<endl;
18 
19     string s4(cstr);   //将C字符串作为s4的初值
20 
21     cout<<s4<<endl;
22 
23     string s5(cstr,3);  //将C字符串前3个字符作为字符串s5的初值。
24 
25     cout<<s5<<endl;
26 
27     string s6(5,'A');  //生成一个字符串,包含5个'A'字符
28 
29     cout<<s6<<endl;
30 
31     string s7(str.begin(),str.begin()+5); //区间str.begin()和str.begin()+5内的字符作为初值
32 
33     cout<<s7<<endl;
34 
35     return 0;

输出:

Hello world


world


wor


abcde


abc


AAAAA


Hello


二.比较

好不容易把各种string赋上你想要的值,现在我们开始比较.

string类型支持><+==这些字符串里根本做不到的比较.

 1  string str;
 2 
 3     cout << "Please input your name:"<<endl;
 4 
 5     cin >> str;
 6 
 7     if( str == "Li" )   // 字符串相等比较
 8 
 9         cout << "you are Li!"<<endl;
10 
11     else if( str != "Wang" )  // 字符串不等比较
12 
13         cout << "you are not Wang!"<<endl;
14 
15     else if( str < "Li")     // 字符串小于比较,>、>=、<=类似
16 
17         cout << "your name should be ahead of Li"<<endl;
18 
19     else
20 
21         cout << "your name should be after of Li"<<endl;
22 
23     str += ", Welcome!";  // 字符串+=
24 
25     cout << str<<endl;
26 
27     for(int i = 0 ; i < str.size(); i ++)
28 
29         cout<<str[i];  // 类似数组,通过[]获取特定的字符
30 
31     return 0;

三.特性函数

string有很多它自己的函数

int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)

int max_size()const;    //返回string对象中可存放的最大字符串的长度

int size()const;        //返回当前字符串的大小

int length()const;       //返回当前字符串的长度

bool empty()const;        //当前字符串是否为空

void resize(int len,char c);  //把字符串当前大小置为len,多去少补,多出的字符c填充不足的部分

用的时候:

str.capacity=...

代码:

 1     string str;
 2 
 3         if (str.empty())//判断是不是空的
 4 
 5         cout<<"str is NULL."<<endl;
 6 
 7         else
 8 
 9         cout<<"str is not NULL."<<endl;
10 
11     str = str + "abcdefg";//string可以直接加
12 
13     cout<<"str is "<<str<<endl;
14 
15         cout<<"str's size is "<<str.size()<<endl;//   str's size is 7(size 是字符数,类似strlen)
16 
17        cout<<"str's capacity is "<<str.capacity()<<endl;//str's capacity is 15  
18 
19     cout<<"str's max size is "<<str.max_size()<<endl;//str's max size is 4294967294
20 
21     cout<<"str's length is "<<str.length()<<endl;//str's length is 7 (其实和size是一样的)
22 
23     str.resize(20,'c');(20个字符,空位补c)
24 
25     cout<<"str is "<<str<<endl;//str is abcdefgccc
26 
27     str.resize(5);(5个字符,多的删掉)
28 
29     cout<<"str is "<<str<<endl;//str is abcde
30 
31     return 0;
 1 #include <iostream> 
 2 #include <string>  3 using namespace std;
 4 int main()
 5  {
 6  string str1 = "abc123defg";
 7  string str2 = "swap!";
 8  cout<<str1<<endl; 
 9 cout<<str1.erase(3,3)<<endl;
10  //从索引3开始的3个字符,即删除掉了"123"
11  cout<<str1.insert(0,"123")<<endl;
12  //在头部插入
13  cout<<str1.append("123")<<endl;
14  //append()方法可以添加字符串
15  str1.push_back('A'); //push_back()方法只能添加一个字符 
16 cout<<str1<<endl;
17  cout<<str1.replace(0,3,"hello")<<endl;
18  //即将索引0开始的3个字符替换成"hello"
19  cout<<str1.substr(5,7)<<endl;
20  //从索引5开始7个字节 
21 str1.swap(str2); 
22 cout<<str1<<endl;
23  const char* p = str.c_str();
24  printf("%s\n",p);
25  return 0;
26  }

 

posted @ 2020-02-18 11:38  安之若醇  阅读(216)  评论(0编辑  收藏  举报
Live2D服务支持