C++标准库之String

C++中支持的字符串处理的函数库叫String,但它不是STL,却与STL操作十分相似。

1.声明:

使用String之前要有以下头文件

#include<string>
using namespace std;

声明方法

string s;   //声明一个string对象 s
string s[10]; //声明一个string对象数组s 

初始化string对象

直接初始化:利用小括号完成;

拷贝初始化:利用等号完成拷贝过程(减少使用);

string s1;            //定义一个空字符 
string s2 = s1;       //拷贝初始化,将s1赋值给s2 
string s3 = "Hello";  //拷贝初始化,用字符串字面值初始化s3 
string s4(10,'c');    //直接初始化,s4内容是cccccccccc 
string s5(s3);        //直接初始化,这是新的初始化方法,等价于s5 = s3 
string s6("World");      //直接初始化,这是新的初始化方法,等价于s6 = “World”

以下是运行结果

//
//
//Hello
//cccccccccc
//Hello
//World

输入字符串方法

cin >> s7;          // 读取有效字符串直到遇到空格 
getline(cin,s8);    // 读取字符串直到遇到换行结束,可读入空格 
getline(cin,s9,'a') // 读取除了'a'以外的所有字符串,包括'\n'和空格 

如果想反复读入字符

string s10;
while(cin >> s10){//其他操作 
}

2.string对象的操作

s.empty();                  //   判断是否为空,返回bool型 
s.size(),s.length();        //   返回字符串的个数 
s[n];                       //   字符串第n-1个字符,从0开始计数 
s1+s2;                      //   将s2字符串连接在s1后面 s = s + '0';            //   在s字符串后面加个字符0 s = s + 'a'             //   在s字符串后面加个字符a string s2 = s1 + "adc"  //   s1字符串后加个字符串"abc"赋值给s2 
s1 = s2;                    //   将s2字符串赋值给s1 
s1 == s2;                   //   判断s2字符串和s1字符串是否相等
!=,<,<=,>,>=                //   两个字符串的比较

3.string对象中字符的处理(头文件cctype

isalnum(c);   //如果c是字母或者数字,返回true 
isalpha(c);   //如果c是字母,返回true 
iscntrl(c);   //如果c是控制字符,返回true 
isdigit(c);   //如果c是数字,返回true 
isgraph(c);   //如果c不是空格,可打印,返回true 
islower(c);   //如果c是小写字母,返回true 
isupper(c);   //如果c是大写字母,返回true
isprint(c);   //如果c是可打印字母,返回true
ispunct(c);   //如果c是标点符号,返回true
isspace(c);   //如果c是空白字符,返回true
isxdigit(c);  //如果c是十六进制数,返回true
tolower(c) ;  //如果c是大写字母,返回小写字母 
toupper(c);   //如果c是小写字母,返回大写字母

4.函数操作

1)assign():

s.assign(base);                           //将base赋值给s 
s.assign(base,10,9);                      //将base第10字符已经后9个字符赋值给s 
s.assign("you are so beautiful",7);       //将字符串中前0-6个字符赋值给s 
s.assign("you are so beautiful");         //将字符赋值给s 
s.assign(10,'*');                         //将10个*赋值给s 
s.assign<int>(10,0x2D);                   //赋值10个-给s 
s.assign(base.begin()+16,base.end()-12);  //将base迭代器中指定位置的字符赋给s 

2)insert():     

  string str="to be question";string str2="the ";string str3="or not to be";string::iterator it;// used in the same order as described above://从0开始计数 str.insert(6,str2);                            // 将字符串str2插入到str的第六个位置(str[5]) str.insert(6,str3,3,4);                        // 将str3第3,4,5,6四个字符插入到str第六个位置处 str.insert(10,"that is cool",8);               // 将"that is cool"字符串中前八个字符插入到str第10个位置处 str.insert(10,"to be ");                       // 将"to be "插入到str第10个位置处 str.insert(15,1,':');                          //  插入一个,到str的第15个位置处 it = str.insert(str.begin()+5,',');            //  将,插入到字符串开头的第五个位置  cout << *it << endl;                           //it指向的是, str.insert (str.end(),3,'.');                  //  在字符串末尾插入3个. str.insert (it+2,str3.begin(),str3.begin()+3); //将str3前3个字符插入到,后面的第2个位置 

 

3)find():                                                            //字符串查找,若存在返回下标。

4)replace():                                                      //替换字符

5)erase():                                                        //删除字符

6)append(),+=,push_back():                       //在尾部添加字符

7)compare(),==,!=,<,<=,>,>=:        //字符串的比较

8)reverse():                                                  //保留一定量内存以容纳一定数量的字符

9)substr():                                                    //返回某个字符串a) 

10) swap()                                                        //交换两个字符串的内容

11) clear()                                                       //删除全部字符 

12) +                                                               //串联字符串

13) size(),length()                                          //返回字符数量

14) max_size()                                              //返回字符的可能最大个数

15) empty()                                                   //判断字符串是否为空

16) capacity()                                               //返回重新分配之前的字符容量

17) [ ], at()                                                    //存取单一字符

18) >>,getline()                                            //从stream读取某值

19) <<                                                          //将值写入stream

20) copy()                                                   //将某值赋值为一个C_string

21) c_str()                                                  //将内容以C_string返回

22) data()                                                  //将内容以字符数组形式返回

23) substr()                                               //返回某个子字符串

24)begin() end()                                      //提供类似STL的迭代器支持

25) rbegin() rend()                                  //逆向迭代器

26) get_allocator()                                  //返回配置器

(未完待续)(以后需要什么再查官方文档)
http://www.cplusplus.com/reference/string/string/

 

转载于:https://www.cnblogs.com/wanghao-boke/p/10440836.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385136.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

652. 寻找重复的子树

给定一棵二叉树&#xff0c;返回所有重复的子树。对于同一类的重复子树&#xff0c;你只需要返回其中任意一棵的根结点即可。 两棵树重复是指它们具有相同的结构以及相同的结点值。 示例 1&#xff1a; 1 / \ 2 3 / / \ 4 2 4 / 4 …

817. 链表组件

给定一个链表&#xff08;链表结点包含一个整型值&#xff09;的头结点 head。 同时给定列表 G&#xff0c;该列表是上述链表中整型值的一个子集。 返回列表 G 中组件的个数&#xff0c;这里对组件的定义为&#xff1a;链表中一段最长连续结点的值&#xff08;该值必须在列表 G…

1121 Damn Single (25 分)

"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of. Input Specification: Each input file contains one test case. For each case,…

1124 Raffle for Weibo Followers (20 分)

John got a full mark on PAT. He was so happy that he decided to hold a raffle&#xff08;抽奖&#xff09; for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are suppose…

987. 二叉树的垂序遍历

给定二叉树&#xff0c;按垂序遍历返回其结点值。 对位于 (X, Y) 的每个结点而言&#xff0c;其左右子结点分别位于 (X-1, Y-1) 和 (X1, Y-1)。 把一条垂线从 X -infinity 移动到 X infinity &#xff0c;每当该垂线与结点接触时&#xff0c;我们按从上到下的顺序报告结点的值…

28. 实现 strStr()

实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在&#xff0c;则返回 -1。 示例 1: 输入: haystack "hello", needle "ll" 输出: 2 示例…

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ with 0 for all i and a​k​​>0. Then N is palindromic if and only if a​i​​a​k−i​​ for all i. Zero is written 0 and is also palindrom…

1044 火星数字 (20 分)

火星人是以 13 进制计数的&#xff1a; 地球人的 0 被火星人称为 tret。地球人数字 1 到 12 的火星文分别为&#xff1a;jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。火星人将进位以后的 12 个高位数字分别称为&#xff1a;tam, hel, maa, huh, tou, kes, he…

43. 字符串相乘

给定两个以字符串形式表示的非负整数 num1 和 num2&#xff0c;返回 num1 和 num2 的乘积&#xff0c;它们的乘积也表示为字符串形式。 示例 1: 输入: num1 "2", num2 "3" 输出: "6" 示例 2: 输入: num1 "123", num2 "456&qu…

1045 快速排序 (25 分)

著名的快速排序算法里有一个经典的划分过程&#xff1a;我们通常采用某种方法取一个元素作为主元&#xff0c;通过交换&#xff0c;把比主元小的元素放到它的左边&#xff0c;比主元大的元素放到它的右边。 给定划分后的 N 个互不相同的正整数的排列&#xff0c;请问有多少个元…

1049 数列的片段和 (20 分)

给定一个正数数列&#xff0c;我们可以从中截取任意的连续的几个数&#xff0c;称为片段。例如&#xff0c;给定数列 { 0.1, 0.2, 0.3, 0.4 }&#xff0c;我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0…

C++ Priemer目录索引

序号内容1 【C Primer | 15】虚函数表剖析&#xff08;一&#xff09; 2 【C Priemr | 15】虚函数表剖析&#xff08;二&#xff09; 3 【C Priemr | 15】虚函数表剖析&#xff08;三&#xff09; 4一个C程序执行main函数前和执行完main函数后会发生什么。1 【C Priemr | 15】虚…

1046 划拳 (15 分)

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为&#xff1a;每人口中喊出一个数字&#xff0c;同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和&#xff0c;谁就赢了&#xff0c;输家罚一杯酒。两人同赢或两人同输则继续下一轮&…

多线程顺序交替打印ABCD

题目&#xff1a;按照 ABCD的顺序交替打印。 1. 测试代码&#xff1a; #include <iostream> #include <unistd.h> #include <stdlib.h> #include <pthread.h> using namespace std;struct {int t;pthread_mutex_t mutex;pthread_cond_t cond; } tes…

第一个只出现一次的字符

在一个字符串(0<字符串长度<10000&#xff0c;全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1&#xff08;需要区分大小写&#xff09;. 解法&#xff1a; class Solution { public:int FirstNotRepeatingChar(string str) {unordered…

《Leetcode》目录

序号题目题解标记1 43. 字符串相乘 字符串2513. 找树左下角的值二叉树3 450. 删除二叉搜索树中的节点 二叉树486. 分隔链表链表155 155. 最小栈 C题解栈77. 组合C题解回溯算法15.三数之和C题解

一个C++程序执行main函数前和执行完main函数后会发生什么。

总结&#xff1a; main函数执行之前&#xff0c;主要就是初始化系统相关资源&#xff1a; 设置栈指针初始化static静态和global全局变量&#xff0c;即data段的内容将未初始化部分的赋初值&#xff1a;数值型short&#xff0c;int&#xff0c;long等为0&#xff0c;bool为FALS…

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then N integers are…

【面试宝典 | 01】面经

字节跳动提前批后端第三面凉经该来的终究会来的

1148 Werewolf - Simple Version (20 分)

Werewolf&#xff08;狼人杀&#xff09; is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game, player #1 said: "Player #2 is a werewolf.";player #2 said: "Player #3 is a h…