【C++札记】标准模板库string
介绍
c++中字符串string对象属于一个类,内置了很多实用的成员函数,操作简单,方便更直观。
命名空间为std,所属头文件<string> 注意:不是<string.h>。
跟进代码会发现string其实只是basic_string模板类的一个typedef。
赋值
//方法1
string str1 = "woniu201";
//方法2
char* p = "woniu201";
string str2 = p;
遍历
//方法1 使用下标
for (int i=0; i<str1.length(); i++)
{
printf("%c", str1[i]);
}
//方法2 使用迭代器
string::iterator it;
for (it=str1.begin(); it!=str1.end(); it++)
{
printf("%c", *it);
}
查找
string str5 = "woniu201";
int pos1 = str5.find("n", 0); //从位置0开始查找字符n在字符串str5中的位置
int pos2 = str5.find("niu", 0); //从位置0开始查找字符串niu在字符串str5中的位置
int pos3 = str5.find("niu", 0, 2);//从位置0开始查找字符串niu前两个字符组成的字符串在str5中的位置
截取
string str3 = "woniu201";
string str4 = str3.substr(0,5);//返回从下标0开始的5个字符组成的字符串
其他
//返回C串char* c_str()
char buf[1024] = {0};
string str5 = "i love chain";
strcpy(buf, str5.c_str());
//字符串连接
string str6 = "woniu201";
string str7 = "hailuo201";
string str8 = str6 + str7;
//判断是否相等
bool bRet1 = (str6 == str7); //相等为true,否则为false
//判断字符串是否为空
bool bRet2 = str6.empty();
//字符串插入
string str9 = str6.insert(0, str7); //字符串str6的0位置插入字符串str7
//字符串交换
str6.swap(str7);
//判断是否包含
string::size_type idx = str6.find("woniu");
if(idx == string::npos)
{
cout << "not found" << endl;
}
欢迎加群交流:C/C++开发交流

【C++札记】标准模板库string的更多相关文章
- C++——string类和标准模板库
一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...
- C++标准模板库Stand Template Library(STL)简介与STL string类
参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...
- (18)C++ string和标准模板库
一.stringl类 1.string构造函数 string a1("abc");//初始化字符串 cout<<a1<<endl;//abc , '#'); ...
- C++ primer plus读书笔记——第16章 string类和标准模板库
第16章 string类和标准模板库 1. string容易被忽略的构造函数: string(size_type n, char c)长度为n,每个字母都为c string(const string ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- 【c++】标准模板库STL入门简介与常见用法
一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...
- STL 简介,标准模板库
这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...
- 标准模板库(STL)学习探究之stack
标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string
随机推荐
- C++11多线程std::thread创建方式
//#include <cstdlib> //#include <cstdio> //#include <cstring> #include <string& ...
- geometry_msgs/PoseStamped 类型的变量的构造
#navpoint.msg geometry_msgs/PoseStamped target_pose uint8 floor uint8 type target_pose 的类型为geometry_ ...
- MQ Cannot convert from [[B] to [] for GenericMessage
MQ消费端转换报错:主要错误信息:Caused by: org.springframework.messaging.converter.MessageConversionException: Cann ...
- 【Python代码】随机抽取文件名列表NameList中的Name作为训练集
#!/usr/bin/env python #coding=utf-8 #随机抽取一部分图片作为测试集 import random NameList=[]#存储所有图片名字 ''' NameListP ...
- 记一次ArrayList产生的线上OOM问题
前言:本以为(OutOfMemoryError)OOM问题会离我们很远,但在一次生产上线灰度的过程中就出现了Java.Lang.OutOfMemoryError:Java heap space异常,通 ...
- 服务端 CORS 解决跨域
当协议.域名.端口中任一个不同时产生跨域 CORS 跨域资源共享(Cross-origin resource sharing) 参考资料https://developer.mozilla.org/zh ...
- bing map for wpf 怎么显示中文地图
添加一下代码: string suriFormat = "http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41"; Mic ...
- sed替换 - 含斜杠(\)和Shell变量
gen_image.bat中的内容如下: FOTARomPacker.exe -i .\_ini\FOTARomPacker.ini -o .\_Output\a.bin @IF %ERRORLE ...
- Intellij-编译
目录 IntelliJ IDEA 编译方式介绍 编译方式介绍 编译触发按钮 运行之前的编译 @(目录) IntelliJ IDEA 编译方式介绍 编译方式介绍 相比较于 Eclipse 的实时自动编译 ...
- MySQL数据库表的设计和优化(上)
一.单表设计与优化: (1)设计规范化表,消除数据冗余(以使用正确字段类型最明显):数据库范式是确保数据库结构合理,满足各种查询需要.避免数据库操作异常的数据库设计方式.满足范式要求的表,称为规范化表 ...