C++STL常见用法(未完成

1、string(*)

概念:相当于char*的封装,理解为字符串

1.1.简单使用
/**C中定义字符串以及打印*/
char *ch="asdkajbf";
for(int i=0;ch[i]!=‘\0‘;i++) cout<<*(ch+i);
/**C++中*/
string s="ssadaffw";
cout<<s<<endl;
1.2.获取一行字符串及长度

用cin读入字符串的时候,是以空格为分隔符的,如果想要读入一整行的字符串,就需要用getline

s 的长度可以用s.length() 获取(有几个字符就是长度多少,不存在char[] 里面的什么末尾的结束之类的)

string s; // 定义一个空字符串s
getline(cin, s); // 读取一行的字符串,包括空格
cout << s.length(); // 输出字符串s的长度
1.3.+=运算符

+=对于字符串、字符有效,数字会转为asc码

1.4.排序(使用algorithm头文件)
string s="5418340";
sort(s.begin(),s.end());
cout<<s;
1.5.insert函数

string &insert(int p0, const char *s);——在p0位置插入字符串s

string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n个字符

string &insert(int p0,const string &s);——在p0位置插入字符串s

string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符

string &insert(int p0, int n, char c);//在p0处插入n个字符c

iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符

void insert(iterator it, int n, char c);//在it处插入n个字符c

string str="to be question";
string str2="the ";
string str3="or not to be";string::iterator it;
str.insert(6,str2);                 // to be (the )question
str.insert(6,str3,3,4);             // to be (not )the question
str.insert(10,"that is cool",8);    // to be not (that is )the question
str.insert(10,"to be ");            // to be not (to be )that is the question
str.insert(15,1,‘:‘);               //加一个‘.‘ to be not to be(:) that is the question
it = str.insert(str.begin()+5,‘,‘); // to be(,) not to be: that is the question
str.insert (str.end(),3,‘.‘);       // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
1.6.erase函数
/*begin是头迭代器,end是尾迭代器*/
string s="5418340";
s.erase(s.begin());//删除第一个
s.erase(--s.end());//删除最后一个
cout<<s;
1.7.substr函数
string s2=s.substr(4); // 表示从下标4开始一直到结束
string s3=s.substr(5,3); // 表示从下标5开始,3个字符
1.8.循环(3种)

1.for循环

string s="5418340";
for(int i=0;i<s.length();i++) cout<<s[i];

2.迭代器

for(string::iterator it=s.begin();it!=s.end();it++) cout<<*it;

3.迭代器化简

for(auto it=s.begin();it!=s.end();it++) cout<<*it;

4.利用C++ 11新特性for循环

for(auto x:s) cout<<x;

原文地址:https://www.cnblogs.com/transmigration-zhou/p/12269811.html

时间: 2024-08-30 14:15:38

C++STL常见用法(未完成的相关文章

STL常见用法

1.选择C++刷算法的理由 1.C++速度快(C不是更快么,java太慢了) 2.C++有STL(什么是STL)--使用很方便的类库 3.如何使用STL进行高效刷算法 4.好处:刷算法,学习成本极低 5.如何从C到C++(仅基础语法到刷算法程度) 俗话说:磨刀不误砍柴工 不会c++仍然可以做,但是效率低 2.输入输出 C++保留了C的scanf和printf,增加了额外的cin与cout 例子 2.1.C程序中输入输出 int a; scanf("%d",&a); printf

IOS中NSString的常见用法

iOS NSString的常用用法 //1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; //3.在以上方法中,提升速度:initWithString方法 NSString *astring = [[NSString allo

python之模块pprint之常见用法

# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块pprint之常见用法 import pprint data = [(1,{'a':'A','b':'B','c':'C','d':'D'}),(2,{'e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L'}),] print '--'*30 #1.打印效果 pprint.pprint (data) ''' ----

.NET中Path类的一些常见用法

.NET为处理文件路径提供了一个Path类,利用该类可以方便的处理文件路径,如更改文件后缀,合并文件路径,改变文件的扩展名等.有一点需要注意的是,Path类本质上是对一个字符串进行处理,更改的只是该字符串,而不会影响实际的文件.下面是该类的一些常见用法示例: 1 string filePath = @"C:\D\log\Receive\postedFile.txt"; 2 int padSpacesLength = 30; 3 string newFilePath = string.E

sscanf的常见用法

例子: 1. 常见用法. char buf[512] = ; sscanf("123456 ", "%s", buf); printf("%s\n", buf); 结果为:123456 2. 取指定长度的字符串.如在下例中,取最大长度为4字节的字符串. sscanf("123456 ", "%4s", buf); printf("%s\n", buf); 结果为:1234 3. 取到指定

正则表达式介绍及常见用法

正则表达式(Regular Expression),又称正规表示法.常规表示法,在实际的软件开发项目中经常会被使用到.它使用单个字符串来描述.匹配并获取一系列符合某个句法规则的结果. 我将从最基础的部分为大家写这个正则表达式教程. 正则表达式起源   1956年,数学家Stephen Kleene在Warren McCulloch和Walter Pitts早期神经系统工作的基础上,设计出了一个数学符号体系--regular sets(规则的集合),这个东西很快被计算机科学家用于编译器的扫描或词法

STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map   list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [unordered_multimap]     contiguous storage double-ended queue LIFO FIFO 1st is greatest  

python之模块poplib之常见用法

# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块poplib之常见用法 ''' 所以,收取邮件分两步: 第一步:用poplib把邮件的原始文本下载到本地: 第二部:用email解析原始文本,还原为邮件对象. poplib.POP3(host,port,timeout):连接pop3服务器 poplib.POP3_SSL(): POP3.set_debuglevel(level):设置调试模式,可以看到与服务器的交互信息 POP3.ge

【ORM】关于Dapper的一些常见用法

引言 Dapper是.Net平台下一款小巧玲珑的开源Orm框架,简单实用的同时保持高性能,非常适合我这种喜欢手写SQL的人使用,下面介绍一下如何使用Dapper. 相关资料 Dapper的GitHub地址是dapper-dot-net,上面有一些用法和性能的介绍,有兴趣者可以上去看看. 数据库设计和实体类     简单设计两个表,分别为部门表,雇员表. CREATE TABLE [dbo].[T_Dept] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Dept]