C++标准库string
C++标准库string
定义和初始化
| string s1 | 默认初始化,s1是一个空串 |
|---|---|
| string s2(s1) | s2是s1的副本 |
| string s2 = s1 | 等价于s2(s1),s2是s1的副本 |
| string s3("value") | s3是字面值"value"的副本,除了字面值最后的那个空字符外 |
| string s3 = "value" | 等价于s3("value"),s3是字面值"value"的副本 |
| string s4(n, 'c') | 把s4初始化为由连续n个字符c组成的串 |
直接初始化和拷贝初始化
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1; // 默认初始化,空字符串
string s2("haha"); // 直接初始化
string s3 = "haha"; // 拷贝初始化
string s4(10, 'a'); // 直接初始化
cout << s1 << endl
<< s2 << endl
<< s3 << endl
<< s4 << endl;
system("pause");
return 0;
}
程序输出结果
haha
haha
aaaaaaaaaa
请按任意键继续. . .
string对象上的操作

读写string对象
- 写入一个字符串。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin >> s;
cout << s << endl;
system("pause");
return 0;
}
程序输出结果
hello world
hello
请按任意键继续. . .
- 写入两个字符串。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
cout << s1 << s2 << endl;
system("pause");
return 0;
}
程序输出结果
hello world
helloworld
请按任意键继续. . .
- 读取未知数量的string对象
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
while (cin >> word)
cout << word << endl;
system("pause");
return 0;
}
程序输出结果
hi
hi
visual
visual
studio
studio
^Z
请按任意键继续. . .
使用ctrl + Z结束输入。
使用getline读取一整行
保留字符串中的空白符,从给定的输入流中读入内容,直到遇到换行符位置,然后把所读的内容存入到string对象中。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while (getline(cin, line))
cout << line;
system("pause");
return 0;
}
程序输出结果
hello hi !!!
hello hi !!!
wooohhh woc
wooohhh woc
^Z
请按任意键继续. . .
- 只输出非空的行
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while (getline(cin, line))
// 只输出非空的行
if(!line.empty())
cout << line;
system("pause");
return 0;
}
- 只输出超过10个字符的行
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
while (getline(cin, line))
// 只输出超过10个字符的行
if (line.size()>10)
cout << line;
system("pause");
return 0;
}
程序输出结果
hello
hello world
hello world
^Z
请按任意键继续. . .
string对象相加
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 = "hello";
string s2 = " world\n";
//string s3 = "hello" + " world\n"; //错误
string s3 = s1 + s2;
string s4 = s1 + " haha\n"; //正确,字面值可以和string对象相加
cout << s3 << endl;
cout << s4 << endl;
system("pause");
return 0;
}
程序运行结果
hello world
hello haha
请按任意键继续. . .
处理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 |
| isprint(c) | 如果c是可打印字符,则为true |
| ispunct(c) | 如果c是标点符号,则为true |
| isspace(c) | 如果c是空白是否,则为true |
| isupper(c) | 如果c是大写字母,则为true |
| isxdigit(c) | 如果c是十六进制的数,则为true |
| tolower(c) | 如果c是大写字母,返回其小写字母的形式,否则直接返回c |
| toupper(c) | 如果c是小写字母,返回其大写字母的形式,否则直接返回c |
件中的函数把string对象中的字符每行打印出来
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 = "some string";
cout << "method a" << endl;
for (auto c : s1)
cout << c << endl;
//that equals to
cout << "method b" << endl;
for (int i = 0; i < s1.size(); ++i)
cout << s1[i] << endl; // 通过下标索引
system("pause");
return 0;
}
程序输出结果
method a
s
o
m
e
s
t
r
i
n
g
method b
s
o
m
e
s
t
r
i
n
g
请按任意键继续. . .
- 统计string对象中标点符号的个数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1 = "hello, world!!!";
decltype(s1.size()) count = 0;
for (auto c : s1)
if (ispunct(c))
++count;
cout << s1 + "中共有"
<<count
<<"个标点符号" << endl;
system("pause");
return 0;
}
程序输出结果
hello, world!!!中共有4个标点符号
请按任意键继续. . .
当然还可以实现其他很多功能,就不一一列举。
C++标准库string的更多相关文章
- C++标准库string类型
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...
- C++标准库<string>简单总结
C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...
- c/c++ 标准库 string
c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...
- 实现C++标准库string类的简单版本
代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- 标准库string与C风格字符串
返回字符串的长度 string标准库 #include<iostream> #include<cstring> using namespace std; int main() ...
- 【C++ Primer每日刷】之三 标准库 string 类型
标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...
- 把《c++ primer》读薄(3-1 标准库string类型初探)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...
- C++ 标准库string字符串的截取
标准库的string有一个substr函数用来截取子字符串.一般使用时传入两个参数,第一个是开始的坐标(第一个字符是0),第二个是截取的长度. #include <iostream> #i ...
随机推荐
- [iOS]从零开始开发一个即时通讯APP
前言 这是我的毕业设计.刚开始确定这个课题的时候是因为以前有稍微研究过一些XMPP协议,在这个基础上做起来应该不难.然后开始选技术的时候还有半年,我想为什么不从更底层做起呢!那就不用XMPP,当时接触 ...
- Winform Timer用法,Invoke在Timer的事件中更新控件状态
System.Timers.Timer可以定时执行方法,在指定的时间间隔之后执行事件. form窗体上放一个菜单,用于开始或者结束定时器Timer. 一个文本框,显示定时执行方法. public pa ...
- .NET C#到Java没那么难,Servlet篇
前言 .NET C#到Java没那么难,都是面向对向的语言,而且语法还是相似的,先对比一下开发环境,再到Servlet,再到MVC,都是一样一样的,只是JAVA的配制项比较多而已,只要配好一个,后面都 ...
- response.sendRedirect页面跳转的方法总结——实习第六天
今天想到要写这个博客完全是因为今天在这上面摔了一跤,同时也意识到了对于一个学Java的人来讲,知其然并且知其所以然是有多么的重要. 今天报了一个错误,讲的是空指针异常,原因就是在response.se ...
- Kotlin基本语法和使用
Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发.与Java相比,Kotlin的语法更简洁.更具表达性,而且提供了更多的特性. Kotlin是使用Java开发者的思维被创 ...
- java注解编程
- 排序算法总结及Java实现
1. 整体介绍 分类 排序大的分类可以分为两种,内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.主要需要理解的都是内排序算法: 内排序可以分为 ...
- MAC 相关操作解析
MAC 相关操作解析 OS 显示桌面 f11 F1~F12 fn + F1~F12 撤销重做 command + z command + shift + z 图片预览 选择图片 空格 上下左右 svn ...
- 前端解读Webview
作为盛行已久的开发方式,Hybrid的相关介绍已经是相当普遍了.不过看到博客园里基本上都是从android或者ios的角度来讲解的,对于h5的前端来说看起来只能是一直半解.感觉有必要从前端的角度来理解 ...
- python中json.loads,dumps,jsonify使用
search_info = {'id':132,'user_role':3} print type(search_info) #输出 <type 'dict'> #转为string用dum ...