1String概念

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

string和char*的比较

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的值



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个字符,返回修改后的字符串



demo

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

// string的初始化
void initString()
{
	cout << "init of string object\n";
	string s1 = "lucifer";
	string s2("zhang");
	string s3 = s2; // 通过拷贝构造函数来初始化对象s3
	string s4(10, 'a');

	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;
	cout << "s3: " << s3 << endl;
	cout << "s4: " << s4 << endl;
	cout << endl;
}

// 遍历string
void ergodic()
{
	cout << "ergodic of string object\n";
	string s = "lucifer";

	// 1数组方式
	for (unsigned int i = 0; i < s.length(); i++) {
		cout << s[i] << ' ';
	}
	cout << endl;

	// 2迭代器方式
	for (string::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << ' ';
	}
	cout << endl;

	// 3at()方法
	try
	{
		for (unsigned int i = 0; i < s.length() + 3; i++) {
			cout << s.at(i) << ' '; // 抛出异常
		}
		cout << endl;
	}
	catch (...)
	{
		cout << "发生异常\n";
	}
	cout << endl;

	// 如果用下标,程序直接就中止了
	/*
	try
	{
		for (unsigned int i = 0; i < s.length() + 3; i++) {
			cout << s[i] << ' ';
		}
		cout << endl;
	}
	catch (...)
	{

	}
	*/
}

// 字符指针和string的转换
void char2string()
{
	string s = "lucifer";

	// string -> char*
	cout << s.c_str() << endl;

	// string的内容copy,buf中
	char buf[128] = { 0 };
	s.copy(buf, 3, 0); // 注意,只copy3个字符,不会变成C风格的字符串
	cout << "buf: " << buf << endl;
	cout << endl;
}

// string的连接
void stringAppend()
{
	string s1 = "lucifer ";
	string s2 = "zhang";
	s1 = s1 + s2;
	cout << "s1: " << s1 << endl;

	string s3 = "333";
	string s4 = "444";
	s3.append(s4);
	cout << "s3: " << s3 << endl;
	cout << endl;
}

// string查找和替换
void stringFindAndReplace()
{
	string s = "lucifer hello lucifer 111 lucifer 222 lucifer 333";

	int index = s.find("lucifer", 0); // 从位置下标0开始查找
	cout << "index: " << index << endl;

	// 求出lucifer出现的次数,每一次出现的数组下标
	int offIndex = s.find("lucifer", 0);
	while (offIndex != string::npos) {
		cout << "off index: " << offIndex << endl;
		offIndex = s.find("lucifer", ++offIndex);
	}

	// 把s中的lucifer替换成大写
	offIndex = s.find("lucifer", 0);
	while (offIndex != string::npos) {
		cout << "off index: " << offIndex << endl;
		s.replace(offIndex, 7, "LUCIFER");
		offIndex = s.find("lucifer", ++offIndex);
	}
	cout << "after replace: " << s << endl;
	cout << endl;
	// after replace: LUCIFER hello LUCIFER 111 LUCIFER 222 LUCIFER 333
}

// 截断(区间删除)和插入
void stringDelete()
{
	string s = "hello lucifer hello";
	string::iterator it = find(s.begin(), s.end(), 'l');
	if (it != s.end()) {
		s.erase(it);
	}
	cout << "after delete 'l': " << s << endl;
	// after delete 'l': helo lucifer hello

	s.erase(s.begin(), s.end()); // 全部删除
	cout << "after delete all: " << s << endl;
	// after delete all:

	string s2 = "zhang";
	s2.insert(0, "lucifer"); // 头插法
	cout << "after insert: " << s2 << endl;
	// after insert: luciferzhang

	s2.insert(s2.length(), "hello"); // 尾插法
	cout << "after insert: " << s2 << endl;
	// after insert: luciferzhanghello
	cout << endl;
}

void stringTransform()
{
	string s = "LuciferZhang";
	transform(s.begin(), s.end(), s.begin(), toupper);
	cout << "s: " << s << endl;
	// s: LUCIFERZHANG

	transform(s.begin(), s.end(), s.begin(), tolower);
	cout << "s: " << s << endl;
	// s: luciferzhang

	cout << endl;
}

int main()
{
	initString();
	ergodic();
	char2string();
	stringAppend();
	stringFindAndReplace();
	stringDelete();
	stringTransform();

	return 0;
}

STL - string(典型操作demo)的更多相关文章

  1. luogu题解P1032字串变换--BFS+STL:string骚操作

    题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...

  2. C++ STL string对象操作汇总

    string对象 C语言只提供了一个char类型用来处理字符,而对于字符串,只能通过字符串数组来处理,显得十分不便.C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为 ...

  3. STL string常用操作指令

    s.insert(pos,args); 在pos之前插入args指定的字符.pos可以是一个下标或一个迭代器.接受下标的版本返回一个指向s的引用;接受迭代器的版本返回指向第一个插入字符的迭代器. s. ...

  4. 【转载】 C++ stl string 操作

        总结一下C++中string的操作,来自〈C++ Primer〉第四版. 1. string对象的定义和初始化: 12345678910111213 string s1; //空串string ...

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

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

  6. Redis系列-存储篇string主要操作函数小结

    通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...

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

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

  8. 浅谈C++ STL string容器

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

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

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

随机推荐

  1. Go 语言基础语法

    Go 标记 Go 程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号.如以下 GO 语句由 6 个标记组成: fmt.Println("Hello, World!") ...

  2. Python3 循环

    Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中while语句的一般形式: while 判断条件: statement ...

  3. 详解BLE 空中包格式—兼BLE Link layer协议解析

    BLE有几种空中包格式?常见的PDU命令有哪些?PDU和MTU的区别是什么?DLE又是什么?BLE怎么实现重传的?BLE ACK机制原理是什么?希望这篇文章能帮你回答以上问题. 虽然BLE空中包(pa ...

  4. Java第2次实验提纲(Java基本语法与类库)

    1. 使用Git克隆(clone)项目到你的Eclipse项目中 见以下参考资料中的3 从码云将项目clone到你的电脑 重要提示: 使用Git来管理你的代码以后,当你在本机Eclipse项目中开始编 ...

  5. Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息

    原文地址:http://www.javatang.com 前一段时间上线的系统升级之后,出现了严重的高CPU的问题,于是开始了一系列的优化处理之中,现在将这个过程做成一个系列的文章. 基本概念 在对J ...

  6. windows10,redhat6.5下python3.5.2使用cx_Oracle链接oracle

    0.序言 项目主要使用oracle但是我不太喜欢其他编程语言,加上可能需要用python部署算法包,从oracle表中读出数据,处理完成后在放回oracle中去,所以在windows上就想到先用pyt ...

  7. 《An Introduction to Signal Smoothing》译文

    最近在做数据平滑相关的工作,正好读到该篇博客,感觉不错,就翻译了一下.原链接:An Introduction to Signal Smoothing 信号平滑简介 噪声无处不在,不管是在采集手机游戏的 ...

  8. ant编译mysql驱动

    修改驱动源码后需要重新编译构建,由于mysql编译需要两个jdk版本且还需要hibernate4和junit,这里记录下. 安装ant. 配置两个jdk,5和8.并修改build.xml配置,如下: ...

  9. Unity角色残影特效

    残影特效在网上有很多例子,比如这个,我参考着自己整合了一下,算是整合了一个比较完整且特别简单易用的出来,只需要一个脚本挂上去无需任何设定就能用. 这里只针对SkinnedMeshRenderer的网格 ...

  10. foxit pdf强制页面视图所有情况都为'合适宽度'

    在左边的书签点击时,有时明明已经设置为合适宽度,但foxit会自动给你变为'合适页面'.真是莫名其妙的设置.好在可以这样更改: