从零开始学C++之标准库类型(一):string 类简介和例程

一、标准库string类型

string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作 ,在VC中直接F1查看

template <
   class CharType,
   class Traits=char_traits<CharType>, 
   class Allocator=allocator<CharType> 
>
class basic_string

typedef basic_string<char> string; 

typedef basic_string<wchar_t> wstring; 


要使用string类型对象,必须包含相关头文件 

#include <string>

using std::string;

string对象的定义和初始化:

string s1; //默认构造函数,s1为空串

string s2(s1); //将s2初始化为s1的一个副本

string s3(“value”); //将s3初始化为一个字符串字面值副本

string s4(n, ‘c’); //将s4初始化为字符‘c’的n个副本


常用成员函数:



例程1:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <iostream>
using  namespace std;


int main( void)
{
    string s1;
    string s2( "abcdefghijkl");
    cout << s2 << endl;

    basic_string< char> s3( "xxx");    // 等价于string s3("xxx");
    cout << s3 << endl;

    string s4( "abcdefg"4);
    cout << s4 << endl;

    string s5(s2,  23);
    cout << s5 << endl;

    string::iterator first = s2.begin() +  1;
    string::iterator last = s2.begin() +  3;

    string s6(first, last);      //[first, last)
    cout << s6 << endl;


     return  0;
}



例程2:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <iostream>
using  namespace std;

int main( void)
{
    string s1( "abcdefdg");
    cout << s1.size() << endl;
    cout << s1.length() << endl;
    cout << s1.empty() << endl;

    cout << s1.substr( 12) << endl;
    cout << s1.substr( 1) << endl;  //等价于s1.substr(1, -1);

    string::size_type pos = s1.find( 'd'1); //位置从0开始算起
     if (pos == string::npos)  //npos = -1
        cout <<  "not found" << endl;
     else
        cout <<  "pos=" << pos << endl;

    pos = s1.rfind( 'd');
     if (pos == string::npos)
        cout <<  "not found" << endl;
     else
        cout <<  "pos=" << pos << endl;


     return  0;
}



例程3:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
using  namespace std;

int main( void)
{
    string s1( "abcdefghijkl");
    s1.replace( 22"AAAAAA");
    cout << s1 << endl;

    s1 =  "abcdefg";
    s1.replace(s1.begin() +  1, s1.begin() +  4"AAAAAA");
    cout << s1 << endl;

    string s2 =  "xyzabc";
    s2.insert( 2"MMMM");  //在位置2之前插入
    cout << s2 << endl;
    s2.append( "6666");
    cout << s2 << endl;

    string s3 =  "111";
    s2.swap(s3);

    cout << s2 << endl;
    cout << s3 << endl;
     return  0;
}



例程4:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <iostream>
using  namespace std;

void fun( char *str)
{
    cout << str << endl;
}

int main( void)
{
    string s1 =  "abc";

    s1[ 1] =  'B';

    cout << s1 << endl;

     const string s2 =  "xyz";
     //s2[1] = 'Y';      Error s2[1] 返回的是 const char&

    string s3 =  "111" + s1 +  "222" ;
    cout << s3 << endl;

     //s3.c_str()
    fun( const_cast< char *>(s3.c_str()));

     return  0;
}



例程5:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
using  namespace std;
int main()
{
    string strinfo =  " //*---Hello World!......------";
    string strset =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    string::size_type first = strinfo.find_first_of(strset);
     if(first == string::npos)
        cout <<  "not find any characters" << endl;
    string::size_type last = strinfo.find_last_of(strset);
     if(last == string::npos)
        cout <<  "not find any characters" << endl;
    cout << strinfo.substr(first, last - first +  1) << endl;
     return  0;
}

输出:Hello World


可以利用find_first_of 等操作去除空格,如去除左空格可以这样:

string s = "   afas";

string drop = " \t";

s.erase(0, s.find_first_not_of(drop));

去除右空格:

string s = "  dsfs  ";

string drop = " \t";

s.erase(s.find_last_not_of(drop)+1);


参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值