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的更多相关文章

  1. [Cpp primer] Library string Type

    In order to use string type, we need to include the following code #include<string> using std: ...

  2. Library string type(2)——关于String的操作

    关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...

  3. setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)

    对于setCharacterEncoding(java.lang.String),这个方法是javax.servlet.ServletRequest和javax.servlet.ServletResp ...

  4. string Type

    Notes from C++ Primer Operations Operations of string support lots of operations of sequential conta ...

  5. Library vector Type

    vector的定义 vector是C++标准库里最常用的容器,它之所以被称为容器,是因为它可以存放多个对象,所有在用一个容器中的对象都应该具有相同的类型. vector也是一个类模板,这也是它能存放多 ...

  6. [Cpp primer] Library vector Type

    #include<vector> using std::vector; //Vector is a container. //It has a collection of same typ ...

  7. Excel Sheet Column Title (STRING - TYPE CONVERTION)

    QUESTION Given a positive integer, return its corresponding column title as appear in an Excel sheet ...

  8. Redis String Type

    Redis字符串的操作命令和对应的api如下: set [key] [value] JedisAPI:public String set(final String key, final String ...

  9. qt+opencv LNK4272:library machine type 'x64' conflicts with target mathine type 'x86'

    运行时报错如上图所示,原因是你使用的opencv库是64位的,qt里面使用的编译器MSVC是32位的,解决方法如下: 将构建套件修改位64bit:

随机推荐

  1. Spring笔记之(一)初探

    对spring框架的学习我是从模拟它的简单实现开始,这样也易于领悟到它的整个框架结构,以下是简单实现的代码: 配置文件:spring.xml <?xml version="1.0&qu ...

  2. POJ1182食物链 (并查集)

    第一反应就是和那个搞基的虫子的题很像(poj2492 http://www.cnblogs.com/wenruo/p/4658874.html),不过是把种类从2变成了3. 错在很白痴的地方,卡了好久 ...

  3. 初次接触Android ActionBar比较烦人的问题[转]

    本文转自:http://blog.csdn.net/u010933209/article/details/40112079 问题一:icon不能正常显示 一直都对actionbar又爱又恨,特别是刚接 ...

  4. [一]初识SpringMVC

    是什么? web开发框架 为什么用? 功能强大 怎么做? 1.导入jar包 2.配置web.xml <?xml version="1.0" encoding="UT ...

  5. Linux Shell多进程并发以及并发数控制

    1. 基础知识准备 1.1. linux后台进程 Unix是一个多任务系统,允许多用户同时运行多个程序.shell的元字符&提供了在后台运行不需要键盘输入的程序的方法.输入命令后,其后紧跟&a ...

  6. 理解C++中函数的返回

    连续几年的C++程序设计课教学中,学生中总有人要求为他们单独解释函数的返回(return)究竟是什么意思.各种书中都会详讲返回值的问题,而学生们掌握的难点却是在返回至何处执行.本文试图通过对一般函数及 ...

  7. Mybatis上路_06-使用Java自动生成

    目录[-] 1.编写Generator执行配置文件: 2.在MyEclipse中建空web项目: 3.编写并执行Java程序: 4.查看并修改生成的文件: 5.测试,使用生成的文件查询: 1)导入My ...

  8. RadHat搭建内网YUM源server

    前言:随着内网linuxserver越来越多,在每台server上安装软件.都要先把安装盘上传上去.在配置本地yum服务,即麻烦又费时.能够在内网的一台linuxserver上安装yum服务,然后其它 ...

  9. 向架构师进军---&gt;怎样编写软件架构文档

    假设你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",增加这个PM.架构师的大家庭 问:为什么要编写软件架构文档,它的优点是什么? 答: 有文档的架构有助于不同利益相关 ...

  10. ubuntu14.04 64bit 安装 &amp;&amp; 破解quartus13.0 记录

    安装文件:Quartus-13.0.0.156-linux.iso             Quartus-13.0.0.156-devices-1.iso 1.挂载:sudo mount -o lo ...