C++ string类学习总结

  C++字符串技术 string类

   string对象在大多数应用中被用来消除对char*指针的使用,支持所期望的一些操作;

  可以转换成char*,保持和现代代码的兼容性,还能自动处理内存管理;

  一些string的实现采用了引用计数,带来了比基于char*的字符串更佳的性能。

  大多数的编译器已经实现了C++标准的std::string类~

 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
/* stlstring.cpp
    C++字符串技术  string类
    string对象在大多数应用中被用来消除对char*指针的使用,支持所期望的一些操作;
    可以转换成char*,保持和现代代码的兼容性,还能自动处理内存管理;
    一些string的实现采用了引用计数,带来了比基于char*的字符串更佳的性能。
    大多数的编译器已经实现了C++标准的std::string类~
*/


#include  <iostream>
#include  <string>

using   namespace  std;

int  main( void )
{
    
//构造字符串,给字符串赋值
    string myString1( "Michael Joessy!" );
    string myString2(
10 'q' );
    string myString3 = 
"Michael Jordan~" ;
    string myString4(myString1);
    string myString5 = myString3;

    cout << myString1 << endl;
    cout << myString2 << endl;
    cout << myString3 << endl;
    cout << myString4 << endl;
    cout << myString5 << endl;

    
//字符串读入和读出
     //string::operator>>  string::operator<<    string::getline
    string str;
    
//getline(cin, str, ' ');
    cout << str << endl;

    
//字符串判断函数
     /*字符串判断为空函数*/
    string empString(
"" );
    
if  (empString.empty())
    {
        cout << 
"empString is empty!"  << endl;
    }
    
/*字符串长度函数*/
    string testString = 
"1234567890" ;
    cout << testString << endl;
    cout << 
"testString length is "  << testString.length() << endl;
    cout << 
"testString size is "  << testString.size() << endl;
    testString.resize(
5 );
    cout << testString << endl;
    cout << 
"testString length is "  << testString.length() << endl;
    cout << 
"testString size is "  << testString.size() << endl;
    
/*提示:length()和size()的功能是一样的,size()是基于STL概念提出来的,而length()还是原来C语言的东西,
            为了更好地符合STL的编程概念,建议使用size()。
    */


    
/*字符串指针函数*/
    string str1(
"023" );
    cout << 
"str1 = "  << str1 << endl;
    cout << 
"str1 = "  << str1.c_str() << endl;       //c_str()返回字符串的指针

    
//增加字符串成员
    string strAppend( "Michael " );
    cout << strAppend << endl;
    
char  szTest[] =  "Jackson" ;
    strAppend.append(szTest);
    cout << strAppend << endl;
    string strInsert = 
" great" ;
    strAppend.append(strInsert, 
0 6 );
    cout << strAppend << endl;
    strAppend.append(strInsert.begin(), strInsert.end());
    cout << strAppend << endl;

    strAppend += 
" person" ;
    cout << strAppend << endl;

    strAppend.push_back(
'!' );
    cout << strAppend << endl;

    
//字符串比较函数
     /*关系运算符 == > >= < <=*/
    string strt1 = 
"Michael" ;
    string strt2 = 
"Michael" ;
    
if  (strt1 == strt2)
    {
        cout << 
"strt1 == strt2"  << endl;
    }
    string strt3 = 
"Michael Joessy" ;
    
if  (strt3 >= strt1)
    {
        cout << 
"strt3 >= strt1"  << endl;
    }
    
/*compare*/
    
if  (strt1.compare(strt2) ==  0 )
    {
        cout << 
"strt1 == strt2"  << endl;
    }
    
if  (strt3.compare(strt1) !=  0 )
    {
        cout << 
"strt3 != strt1"  << endl;
    }

    
//字符串查找
    string strfind( "abcdefg" );
    
int  nPos =  0 ;
    nPos = strfind.find(
'c' );
    cout << 
"The char c pos is "  << nPos << endl;
    
char  szArray[] =  "def" ;
    nPos = strfind.find(szArray);
    cout << 
"The szArray[] pos is "  << nPos << endl;
    
char  szArrayNone[] =  "drf" ;
    nPos = strfind.find(szArrayNone);
    cout << 
"The szArrayNone[] pos is "  << nPos << endl;

    nPos = strfind.rfind(
'c' );
    cout << 
"The char c pos is "  << nPos << endl;
    nPos = strfind.rfind(szArray);
    cout << 
"The szArray[] pos is "  << nPos << endl;
    nPos = strfind.rfind(szArrayNone);
    cout << 
"The szArrayNone[] pos is "  << nPos << endl;

    string strCha = 
"aaabbbcccdddeeefffggghhh" ;
    nPos = strCha.find_first_of(
'b' );
    cout << 
"The find_first_of b pos is "  << nPos << endl;
    nPos = strCha.find_first_not_of(
'b' );
    cout << 
"The find_first_not_of b pos is "  << nPos << endl;
    nPos = strCha.find_last_of(
'b' );
    cout << 
"The find_last_of b pos is "  << nPos << endl;
    nPos = strCha.find_last_not_of(
'b' );
    cout << 
"The find_last_not_of b pos is "  << nPos << endl;

    
//字符串下标和字串函数
    string strSub =  "Hello World!" ;
    cout << strSub << endl;
    
char  ch1 = strSub[ 6 ];
    
char  ch2 = strSub.at( 11 );
    strSub.at(
11 ) =  '~' ;
    cout << strSub << endl;
    strSub.assign(
"Hello Man!" );
    cout << strSub << endl;

    cout << strSub.substr(
6 9 ) << endl;

    
//字符串其它函数
    string strOrg =  "Hello Hero!" ;
    cout << strOrg << endl;
    strOrg.insert(
0 "At " );
    cout << strOrg << endl;
    cout << strOrg.capacity() << endl;
    cout << strOrg.size() << endl;
    strOrg.replace(
0 2 "Ha" );
    cout << strOrg << endl;

    cin.get();
    
return   0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值