Library string Type
The string type supports variable-length character strings.The library takes cares of managing memory associated with storing the characters and provides various useful operations.The library string type is intended to be efficient enough for general use.
To use string,we need to include the header of string:
#include<string>
using std::string;
Another more efficient way to include the string header is:
#include<cstring> //The c indicates that the header originally comes from the C library
Defining and Initializing strings
- string s1; Default constructors;s1 is the empty string
- string s2(s1); Initialize s2 as a copy of s1
- string s3(“value”); Initialize s3 as a copy of the string literal
- string s4(n,”c”); Initialize s4 with n copies of the character ‘c’
Reading and Writing strings
Here is a simple example:
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
/*
*All of the things above could be replaced by
*#include<iostream>
*#include<cstring>
*using namespace std;
*/
int main()
{
string s;//empty string
cin>>s;//read whitespace-separatedstring to s
cout<<s<<endl;//write s to the output
return 0;
}
This program begins by defaulting a string named s.The next line,
cin>>s;
reads the standard input storing what is read into s.The string input operator:
- Reads and discards any leading whitespace(e.g.spaces,newlines,tabs)
- It then reads characters until the next white whitespace character is encountered.
So,if the input to the program is ” Hello World! “,(note leading and trailing spaces) then the output will be “Hello” with no extra spaces.
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s1,s2;
cin>>s1>>s2; //s1 gets "hello" and s2 gets "world!"
cout<<s1<<s2<<endl;
return 0;
}
If we give this version of the program the same input as in the previous paragraph,our output would be
HelloWorld!
Reading an Unknown Number of Strings
//Note:#include and using declarations must be added to compile this code
int main()
{
string word;
while(cin>>word)
cout<<word<<endl;
return 0;
}
For example,if we input “Welcome to Github!”,then we get
Welcome
to
Github!
In this case,we read into a string using the input operation.The operator returns the istream from which it reads,and the while condition tests the stream after the read completes.If the stream is valid—it hasn’t hit end-of-file or encounter an invalid input—then the body of the while is excuted and the value we read is printed to the standard output.Once we hit end-of-file,we fall out of while.
Using getline to Read an Entire Line
There is an addtional useful string IO operation:getline.The getline function reads the next line of input from the stream and stores what what it read,not including the new line,in its string argument.Whenever getline encounter a new line,even if it’s the first character in the input,it stops reading the input and returns.
int main()
{
string line;
while(getline(cin,line))
cout<<line<<endl;
return 0;
}
For example,if our input is “Welcome to Github!”,then we get
Welcome to Github!
If our input is:
Welcome to Github!
New bee!
then we still get
Welcome to Github!
//第一次全英文,敲得我好累。这篇博客就当是试水吧。要是太花时间,以后就不这么写了…
//ps:感觉有道云笔记的markdown排版更好看啊,是错觉么。。。
Library string Type的更多相关文章
- [Cpp primer] Library string Type
In order to use string type, we need to include the following code #include<string> using std: ...
- Library string type(2)——关于String的操作
关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...
- setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)
对于setCharacterEncoding(java.lang.String),这个方法是javax.servlet.ServletRequest和javax.servlet.ServletResp ...
- string Type
Notes from C++ Primer Operations Operations of string support lots of operations of sequential conta ...
- Library vector Type
vector的定义 vector是C++标准库里最常用的容器,它之所以被称为容器,是因为它可以存放多个对象,所有在用一个容器中的对象都应该具有相同的类型. vector也是一个类模板,这也是它能存放多 ...
- [Cpp primer] Library vector Type
#include<vector> using std::vector; //Vector is a container. //It has a collection of same typ ...
- Excel Sheet Column Title (STRING - TYPE CONVERTION)
QUESTION Given a positive integer, return its corresponding column title as appear in an Excel sheet ...
- Redis String Type
Redis字符串的操作命令和对应的api如下: set [key] [value] JedisAPI:public String set(final String key, final String ...
- qt+opencv LNK4272:library machine type 'x64' conflicts with target mathine type 'x86'
运行时报错如上图所示,原因是你使用的opencv库是64位的,qt里面使用的编译器MSVC是32位的,解决方法如下: 将构建套件修改位64bit:
随机推荐
- win7 下恢复“经典任务栏”/“快速启动栏”,关闭“窗口自动最大化” -摘自网络
1.自动放大窗口 鼠标把窗口拖到屏幕边缘时,win7会自做聪明的把窗口放大,有时候这个很烦人. 解决办法: 运行“REGEDIT”打开注册表,找到 “HKEY_CURRENT_USER\Control ...
- Linux中find、grep命令详细用法
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...
- 联通超级战舰W910 Root 后不能 上网 解决方案
联通版的超级战舰w910root后不能上网,一下是root方法: 超级战舰W910 Root方法: 电脑上安装“刷机精灵”(http://www.shuame.com/ ),在手机“菜单”——“系统设 ...
- JavaScript- 获得TreeView CheckBox里选中项的值
获得TreeView CheckBox里选中项的值,对JSDOM控制还不是很熟,感觉不太容易.试了很多次终于成功了. 代码如下 <body> <form id="form1 ...
- C#- Winform最小化到托盘
实现前先拉一个notifyIcon控件,在Icon属性中加入一个ICON小图标,然后具体的代码实现如下: using System; using System.Collections.Generic; ...
- 【面试虐菜】—— Oracle知识整理《收获,不止Oracle》
普通堆表不足之处: 表更新有日志开销 表删除有瑕疵 表记录太大检索较慢 索引回表读开销很大 有序插入难有序读出 DELETE产生的undo最多,redo也最 ...
- 单位内部DNS架设及域名解析服务
越来越多的企业将企业内部局域网通过光缆.交换机等高速互连设备连接起来,形成较大规模的中型网络,网络上的主机和用户也随之日渐增多.作为 Internet的缩影,企业内部网上的各类服务器(如WWW服务器. ...
- 用WebCollector爬取站点的图片
用WebCollector爬取整站图片,仅仅须要遍历整站页面.然后将URL为.jpg.gif的页面(文件)保存到本地就可以. 比如我们爬取一个美食站点,获取里面全部的图片: import cn.edu ...
- 【转】C++中的虚函数的实现
转自:http://blog.csdn.net/haoel/article/details/1948051 对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(V ...
- JSF学习四 标签
commandButton:提交.重置或下压button button:用于公布GET请求的按钮