10.2.1 STL的string

1String概念

²  string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别呢。

string和char*的比较

c语言没有字符串,使用字符数组模拟字符串。

²  string是一个类, char*是一个指向字符的指针。

string封装了char*,管理这个字符串,是一个char*型的容器。

²  string不用考虑内存释放和越界。

string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

²  string提供了一系列的字符串操作函数(这个等下会详讲)

查找find,拷贝copy,删除erase,替换replace,插入insert

2string的构造函数

²  默认构造函数:

string();       //构造一个空的字符串string s1。

²  拷贝构造函数:

string(const string &str);         //构造一个与str一样的string。如string s1(s2)。

²  带参数的构造函数

string(const char *s);    //用字符串s初始化

string(int n,char c);    //用n个字符c初始化

3string的存取字符操作

²  string类的字符操作:

const char &operator[] (int n) const;

const char &at(int n) const;

char &operator[] (int n);

char &at(int n);

²  operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。

主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。

4从string取得const char*的操作

²  const char *c_str() const;   //返回一个以'\0'结尾的字符串的首地址

5把string拷贝到char*指向的内存空间的操作

²  int copy(char *s, int n, int pos=0) const;

把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。

6string的长度

int length() const;   //返回当前字符串的长度。长度不包括字符串结尾的'\0'。

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

7string的赋值

string &operator=(const string &s);//把字符串s赋给当前的字符串

string &assign(const char *s); //把字符串s赋给当前的字符串

string &assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串

string &assign(const string &s);  //把字符串s赋给当前字符串

string &assign(int n,char c);  //用n个字符c赋给当前字符串

string &assign(const string &s,int start, int n);  //把字符串s中从start开始的n个字符赋给当前字符串

8string字符串连接

string &operator+=(const string &s);  //把字符串s连接到当前字符串结尾

string &operator+=(const char *s);//把字符串s连接到当前字符串结尾

string &append(const char *s);    //把字符串s连接到当前字符串结尾

string &append(const char *s,int n);  //把字符串s的前n个字符连接到当前字符串结尾

string &append(const string &s);   //同operator+=()

string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾

string &append(int n, char c);   //在当前字符串结尾添加n个字符c

9string的比较

int compare(const string &s) const;  //与字符串s比较

int compare(const char *s) const;   //与字符串s比较

compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

10string的子串

string substr(int pos=0, int n=npos) const;    //返回由pos开始的n个字符组成的子字符串

11string的查找 和 替换

查找

int find(char c,int pos=0) const;  //从pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos=0) const;  //从pos开始查找字符串s在当前字符串的位置

int find(const string &s, int pos=0) const;  //从pos开始查找字符串s在当前字符串中的位置

find函数如果查找不到,就返回-1

int rfind(char c, int pos=npos) const;   //从pos开始从后向前查找字符c在当前字符串中的位置

int rfind(const char *s, int pos=npos) const;

int rfind(const string &s, int pos=npos) const;

//rfind是反向查找的意思,如果查找不到, 返回-1

替换

string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s

string &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后在pos处插入串s

void swap(string &s2);    //交换当前字符串与s2的值

//4 字符串的查找和替换

void main25()

{

string s1 = "wbm hello wbm 111 wbm 222 wbm 333";

size_t index = s1.find("wbm", 0);

cout << "index: " << index;

//求itcast出现的次数

size_t offindex = s1.find("wbm", 0);

while (offindex != string::npos)

{

cout << "在下标index: " << offindex << "找到wbm\n";

offindex = offindex + 1;

offindex = s1.find("wbm", offindex);

}

//替换

string s2 = "wbm hello wbm 111 wbm 222 wbm 333";

s2.replace(0, 3, "wbm");

cout << s2 << endl;

//求itcast出现的次数

offindex = s2.find("wbm", 0);

while (offindex != string::npos)

{

cout << "在下标index: " << offindex << "找到wbm\n";

s2.replace(offindex, 3, "WBM");

offindex = offindex + 1;

offindex = s1.find("wbm", offindex);

}

cout << "替换以后的s2:" << s2 << endl;

}

12String的区间删除和插入

string &insert(int pos, const char *s);

string &insert(int pos, const string &s);

//前两个函数在pos位置插入字符串s

string &insert(int pos, int n, char c);  //在pos位置 插入n个字符c

string &erase(int pos=0, int n=npos);  //删除pos开始的n个字符,返回修改后的字符串

13string算法相关

void main27()

{

string s2 = "AAAbbb";

transform(s2.begin(), s2.end(), s2.begin(), toupper);

cout << s2 << endl;

string s3 = "AAAbbb";

transform(s3.begin(), s3.end(), s3.begin(), tolower);

cout << s3 << endl;

}

#include <iostream>
using namespace std;
#include "string"
#include "algorithm" //string的初始化
void main21()
{
string s1 = "aaaa";//s1对象
string s2("bbbb");//s2对象
string s3 = s2;//通过拷贝构造函数 来初始化对象s3
string s4(, 'a');//10个a cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
} //string的 遍历
void main22()
{
string s1 = "abcdefg"; //1 数组方式,string类重载了数组操作符,因此可以像数组操作
for (int i=; i<s1.length(); i++)
{
cout << s1[i] << " ";
}
cout << endl; //2 迭代器是一个指针
for (string::iterator it = s1.begin(); it != s1.end(); it++ )
{
cout << *it << " ";
}
cout << endl; try
{
for (int i=; i<s1.length() + ; i++)
{
cout << s1.at(i) << " "; //at方法出现越界错误时抛出异常
}
}
catch ( ... )
{
cout << "发生异常\n" ;
} cout << "at之后" << endl; try
{
for (int i=; i<s1.length() + ; i++)
{
cout << s1[i] << " "; //[]出现越界错误时不向外面抛出异常 ,因此引起程序的中断
}
}
catch ( ... )
{
cout << "发生异常\n" ;
}
} //字符指针和string的转换,string类封装了字符指针
void main23()
{
//0 char *====>sting
string s1 = "aaabbbb";
//1 string===>char *
printf("s1:%s \n", s1.c_str());//返回string的字符指针
//2 s1的内容 copy到buf中,buf1是char*类型,int copy(char *s, int n, int pos=0) const;
char buf1[] = {};
s1.copy(buf1, , ); //注意 s1给buf1 copy3个字符 不会变成C风格的字符串
scout << "buf1:" << buf1 << endl;
} //字符串的 连接
void main24()
{
string s1 = "aaa";
string s2 = "bbb";
s1 = s1 + s2;
cout << "s1:" << s1 << endl;
string s3 = "";
string s4 = "";
s3.append(s4);
cout << "s3:" << s3 << endl;
} //字符串的查找和替换
void main25()
{
string s1 = "wbm hello wbm 111 wbm 222 wbm 333 ";

//第一次 出现wbm index
int index = s1.find("wbm", ); //位置下标 从0开始
cout << "index: " << index << endl;
//案例1 求wbm出现的次数 每一次出现的数组下标
int offindex = s1.find("wbm", );
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
offindex = offindex + ;
offindex = s1.find("wbm", offindex); //wang bao ming
}
//案例2 把小写wbm===>WBM
string s3 = "aaa bbb ccc";
s3.replace(, , "AAA");//删除0-3,然后插入AAA
cout << "s3" << s3 << endl;
offindex = s1.find("wbm", );
while (offindex != string::npos)//偏移量不等于-1
{
cout << "offindex:" << offindex << endl;
s1.replace(offindex,, "WBM");
offindex = offindex + ;
offindex = s1.find("wbm", offindex); //
}
cout << "s1替换后的结果: " << s1 << endl;
} //截断(区间删除)和插入
void main26()
{
string s1 = "hello1 hello2 hello1";
string::iterator it = find(s1.begin(), s1.end(), 'l');//返回迭代器指针的位置
if (it != s1.end() )
{
s1.erase(it);//删除第一个L
}
cout << "s1删除l以后的结果:" << s1 << endl;
s1.erase(s1.begin(), s1.end() );
cout << "s1全部删除:" << s1 << endl;
cout << "s1长度 " << s1.length() << endl;//空串,长度为0
string s2 = "BBB";
s2.insert(, "AAA"); // 头插法
s2.insert(s2.length(), "CCC");//尾插
cout << s2 << endl;
} void main27()
{
string s1 = "AAAbbb";
//toupper处可以为1函数的入口地址 2函数对象 3预定义的函数对象,toupper转换成大写字母,第二个s1.begin()表示把结果放的位置。
transform(s1.begin(), s1.end(),s1.begin(), toupper);
cout << "s1" << s1 << endl; string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), tolower);
cout << "s2:" << s2 << endl; } void main2222()
{
//main21();
//main22();
//main23();
//main24();
//main25();
//main26();
main27();
cout<<"hello..."<<endl;
system("pause");
return ;
}

stl string的更多相关文章

  1. 深入剖析 linux GCC 4.4 的 STL string

    转自: 深入剖析 linux GCC 4.4 的 STL string 本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Wri ...

  2. 格式字符串分配stl::string

    代码非常easy,不解释,直接在代码: #include <cstdio> #include <cstdarg> #include <iostream> using ...

  3. 浅谈C++ STL string容器

    浅谈C++ STL string容器 本篇随笔简单讲解一下\(C++STL\)中\(string\)容器的使用方法及技巧. string容器的概念 其实\(string\)并不是\(STL\)的一种容 ...

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

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

  5. 转C++之stl::string写时拷贝导致的问题

    前几天在开发某些数据结构到文件的 Dump 和 Load 功能的时候, 遇到的一个 bug . [问题复现] 问题主要出在 Load 过程中,从文件读取数据的时候, 直接使用 fread 的去操作 s ...

  6. [转载] C++ STL string的Copy-On-Write技术

    原文: http://coolshell.cn/articles/12199.html stl的string是经过严格优化的, 深入理解对以后编程过程中应用string非常有益处, 感谢左耳朵耗子的精 ...

  7. [转] 深入剖析 linux GCC 4.4 的 STL string

    本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Write技术. 平台:x86_64-redhat-linux gcc ver ...

  8. STL - string(典型操作demo)

    1String概念  string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都可以用来表示字符串,那么二者有什么区别呢 ...

  9. STL——string

    C++之string类型详解 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个泛型类出现,他集成的操作函 ...

随机推荐

  1. 动画 -- ListView列表item逐个出来(从无到有)

    import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; publi ...

  2. Matlab编程实例(4) 相位角与相关系数曲线

    %相位角与相关系数曲线 close all; clear all; Samp1=200;  %设置信号的采样精度 Samp2=200;  %设置相位角p分割精度 A=10;%信号幅值 w=1;%信号角 ...

  3. LightOJ 1038-Race to 1 Again(概率dp)

    题意: 给你一个数n每一步这个数可以变为他的因子,直到这个数变为1,求n变到1的期望步数. 分析: dp[i],表示i变为1的期望步数,dp[1]=0,dp[n]是答案. dp[i]=sum(dp[j ...

  4. 查看系统或者Jmeter的Properties

    工作台-非测试元件-Property Display,可以显示系统或者Jmeter的Properties

  5. ASP.NET将word文档转换成pdf的代码

    一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...

  6. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...

  7. IE 8兼容:X-UA-Compatible的解释

    来源:http://www.ido321.com/940.html 来自StackOverFlow 问题描述: 1: <meta http-equiv="X-UA-Compatible ...

  8. mysql怎么让一个存储过程定时执行

    比如说每天的12:30执行 查看event是否开启: show variables like '%sche%'; 将事件计划开启: set global event_scheduler=1; 关闭事件 ...

  9. 使用MATLAB生成模糊控制的离线查询表

    1.打开模糊控制工具箱,编辑输入输出变量的隶属度函数和模糊控制规则,如下图所示,导出为fuzzy_control.fis文件. 2.打开Simulink模块,建立下图所示的系统框图,两输入,一输出,处 ...

  10. Hadoop-Map/Reduce实现实现倒排索引

    先来简单介绍一下什么是文档倒排索引 倒排索引是文档检索系统中最常见的数据结构,被广泛应用在全文搜索引擎上.主要用来存储某个单词(或词组)在一个文档或者一组文档中的存储位置的映射,即提供了一种根据内容来 ...