一、标准库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, 2, 3);
    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(1, 2) << 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(2, 2, "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++编程规范

string 类简介和例程的更多相关文章

  1. map 类简介和例程

    一.标准库的map类型 使用map得包含map类所在的头文件 template < class Key, class Type, class Traits = less<Key>, ...

  2. vector 类简介和例程

    一.标准库的vector类型 vector是同一种类型的对象的集合 vector的数据结构很像数组,能非常高效和方便地访问单个元素 vector是一个类模板(class template) vecto ...

  3. C++ string 类详解

    字符串是存储在内存的连续字节中的一系列字符.C++ 处理字符串的方式有两种,一种来自 C 语言,常被称为 C-风格字符串,另一种是基于 string 类库的字符串处理方式.C 风格字符串的处理可以参考 ...

  4. Java基础篇(02):特殊的String类,和相关扩展API

    本文源码:GitHub·点这里 || GitEE·点这里 一.String类简介 1.基础简介 字符串是一个特殊的数据类型,属于引用类型.String类在Java中使用关键字final修饰,所以这个类 ...

  5. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  6. object类的equals方法简介 & String类重写equals方法

    object类中equals方法源码如下所示 public boolean equals(Object obj) { return this == obj; } Object中的equals方法是直接 ...

  7. 【Java】整理关于java的String类,equals函数和比较操作符的区别

    初学 Java 有段时间了,感觉似乎开始入了门,有了点儿感觉但是发现很多困惑和疑问而且均来自于最基础的知识折腾了一阵子又查了查书,终于对 String 这个特殊的对象有了点感悟大家先来看看一段奇怪的程 ...

  8. Spring Security——核心类简介——获得登录用户的相关信息

    核心类简介 目录 1.1     Authentication 1.2     SecurityContextHolder 1.3     AuthenticationManager和Authenti ...

  9. (转)C++——std::string类的引用计数

    1.概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功课,于是你把自己关在房间里 ...

随机推荐

  1. HDU 1171 Big Event in HDU (多重背包变形)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. 【PHP内存泄漏案例】PHP对象递归引用造成内存泄漏

    [案例一] 作者:老王 如果PHP对象存在递归引用,就会出现内存泄漏.这个Bug在PHP里已经存在很久很久了,先让我们来重现这个Bug,代码如下: <?php class Foo { funct ...

  3. SQL Server 2000 ——DBCC命令

    http://blog.163.com/ruifeng_00/blog/static/6904584200971291923462/   一.定义 微软当初从SYBASE将DBCC是作为数据库一致性检 ...

  4. iTunes Connect App Video

    系统要求: 系统升级为 OS X Yosemote 版本 10.10 (正式版已经发布更新) 录制工具: QuickTime Player 版本 10.4 (833) 操作流程: 1. 设备数据线连接 ...

  5. TurboLinux11system»adjtimex简介

    Adjtimex介绍 linux 系统有两个时钟:一个是由主板电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,硬件时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行 ...

  6. 卷积神经网络CNN 手写数字识别

    1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...

  7. OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

    使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 目标 在本教程中我们将涉及以下内容: 使用函数 findHomography 寻找匹配上的关键点的变换. 使用函数  ...

  8. Struts2数据验证机制

    1. 手动验证的实现 只需要在继承ActionSupport类的情况下,直接重写validate()方法即可.使用validate()方法可以对用户请求的多个Action方法进行验证,但其验证的逻辑是 ...

  9. Nginx发展现状及未来特性

    Nginx ("engine x")是一个高性能的HTTP和反向代理 服务器,也是一个IMAP/POP3/SMTP 代理服务器,其特点是占用内存少,并发能力强.到目前完为止,Ngi ...

  10. iOS:二叉树多级表格的使用,使用三方库TreeTableView-master实现对json解析数据的递归遍历整理成树状结构

    在项目中,我们有时需要使用二叉树来实现多级表格的递归遍历查询,如果对二叉树比较懂,那么写起来其实也不费事,为了节省开发时间,下面介绍一下第三方库TreeTableView-master,这个三方库上给 ...