[C++] String Basic
Namespace Declarations
A using declaration let us use a name from a namespace without qualify the name with a namespace_name:: prefix.
// using namespace::name
using std::vector
using std::string
A sperate using declaration is required for each name.
Header should not include using declaration.
String
Initialize string
string s1; // default initialization; s1 is the empty string
string s2 = s1;
string s3 = "hello";
string s4(, 'c'); // s4 is cccccccccccccc
When we initialize a variable using =, we are asking the comiler to copy initialize the object by copying the initilizer on the right-hand side into the object being created. Otherwise, when we omit the =, we use direct initialization.
string s4 = string(, 'c'); // copy initialization; lower efficiency
It is the same as:
string tmp(, 'c'); // tmp is cccccccccc
string s4 = tmp; // copy tmp to s4
Reading and Writing string
int main(){
string , s2;
cin >> s1 >> s2;
cout << s1 << s2 << endl;
}
The string input operator reads and discards any leading whitespace(e.g. spaces, newline, tabs). It then reads characters until the next whitespace character is encountered. So, if the input to the program is "Hello World!", the output will be "HelloWorld!".
Like the input operator, getline() return its istream argument. As a result, we can use getline() as a condiction just as we use the input operatocr as a condiction.
string line;
while (getline(cin, line){
cout << line << endl;
}
The newline that causes getline to return is discarded; the newline is not stored in the sting. Because line don't contain a newline, we must write our won. As usually, we use endl as a newline, to end the current line and flush the buffer.
Because string::size() return an unsigned_type, it is essentual to remember that expression that mis signed and unsigned data can have surpring results.
For example, if n is an int that holds a negative, then s.size() < n will almost surely evaluate as true. It yield true because the negative value in n will convert to a large unsigned value.
You can avoid problems due to conversion between unsigned and int by not using ints in expressions that use size().
Assignment for string
In general, the library type strives to make it as easy to use library type as it is to use a built-in type. We can assigne one string object to another.
string s1(, 'c'), s2; // s2 is empty string
s1 = s2; // replace the content of s1 with a copy of s2 -- empty string
The string library let us convert both character literals and string literals to strings.
string s1= "hello", s2 = "world";
string s3 = s1 + ',' + s2 + "!";
When we mix string and string or character literals, at least one operand to each + operator must be of string type.
string s6 = s1 + ", " + "world!"; // ok
string s7 = "hello" + ", " + s1; // error
s6 works in much the same way as when we chain together input or output expressions. s7 is illegal.
For historical reason, and for compatibility with C, string literals are not standard library strings.
Advise: Used the C++ version of C library Headers.
In addition to facilities defined specially for C++, the C++ library incorporate the C library. Headers in C have names of the form name .h. The C++ version's of these versions header are name c-name - they remove the .h suffix, and precede the name with the letter c.
Hence, cctype has the same content as ctype.h, but in a form that is appropriate for C++ programs.
The range-base for to process every character.
string str("some thing");
for ( auto c: str){
cout << c << endl;
}
On each iteratoion, the next character in str will be copied into c. The changes in c will not impact the content of str.
for (auto &c: str){
c = toupper(c);
}
cout << c << endls;
Remember that reference is just another name for the giving object. When we use a reference as our control varaible, the variable is bound to each element in the sequence in turn. we can change the character to which the reference is bound.
There are two ways to access individual characters in a string: a subscript or an iterator.
The subscript operator(the [] operator) return a reference to the character at the given position. The values we use to subscript a string must be >=0 and < size();
The return of using an index outside the range is undefined.
if (!s.empty()){
cout << s[] << endl;
}
Any time we use a subscript, we must ensure the index is in range of the string's length.
Reference:
C++ Primer, Fifth Edition, 3.2. Library string Type
[C++] String Basic的更多相关文章
- c# post basic 接口
string url = "http://xxxxxxxxx";//地址,请反复检查地址的准确性 string usernamePassword = "username: ...
- 2. Retrofit2 -- Basic Authentication on Android
2. Retrofit2 -- Basic Authentication on Android android Retrofit tutorial 整合基本的认证 Retrofit 1.9 Retro ...
- 在ABP中创建Person实体类
经过之前的准备目前我们的项目,终于可以搞正式的开发工作了. 创建实体Person 在Core类库中添加Person类 /// <summary> /// 联系人 /// </summ ...
- java设计模式之建造者模式
学习了设计模式,一直感觉有进步又没有进步,与同学.同事探讨了一下.变化不可能一会就可以的,需要努力坚持.不管进步大小,也不管是否进步,做到勿忘初心,做自己喜欢的事情就好.还有几个设计模式一直没有写,原 ...
- go access database demo
package main import ( "database/sql" "fmt" _ "github.com/lib/pq" " ...
- go liteIDE
go liteIDE 1 COMM FILE package pricetable import ( "fmt" "math" "os" ...
- http协议Authorization认证方式在Android开发中的使用
我们都知道,http协议是一种无状态协议,在Web开发中,由于Session和Cookie的使用,使得服务端可以知道客户端的连接状态,即用户只需要在浏览器上登录一次,只要浏览器没有关闭,后续所有的请求 ...
- 用 for/in 在 Java 5.0 中增强循环
这个方便的构造提供了什么?什么时候适用于代码? Brett McLaughlin (brett@newInstance.com), 作者/编辑, O'Reilly Media, Inc. 简介: fo ...
- 1111 WordReplace
#include<iostream> #include<string> using namespace std; int main() { string sa,sb,s; wh ...
随机推荐
- 工具 | Axure基础操作 No.1
Axure作为一款热门的原型设计工具,是产品汪必备的一个技能.对于我个人来说,虽然更加喜欢墨刀这种小清新并且易用的网页版轻量级工具. 我在这里进行一些简单操作的动图,方便和我一样刚入门的同学容易看得明 ...
- Paths with -a does not make sense.
最近开始使用为windows的系统,进行git操作的时候出现了一个小问题. 使用命令: E:\IdeaProjects\mmall>git commit -am 'first commit in ...
- 关于端口冲突的解决方式Error: listen EACCES 0.0.0.80
笔者昨天下午临走前安装了vs 2017想要运行一下项目的NET后端来让本机的前端直接对接后端,但是没注意到运行vs后IIS直接占用了本机的80端口.第二天跑nodeJS的时候直接Error: list ...
- BZOJ2037: [Sdoi2008]Sue的小球(区间DP)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 869 Solved: 483[Submit][Status][Discuss] Description ...
- UIPickerView的简单使用
UIPickerView是一个选择器它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,使用也比较简单.下面做了一个关于天气预报的小Demo 用 UI ...
- angularjs中控制器之间的通信----$on、$emit和$broadcast解析
$on.$emit和$broadcast使得event.data在controller之间的传递变的简单. $emit只能向parent controller传递event与data $broadca ...
- CentOS7 搭建GIT环境
一. 安装 libiconv 这个是非常规项 如果服务器原来已经装过 可以不装 但是如果git安装时候提示找不到libiconv 需要在git的安装参数中指定安装路径 最新版是 1.15 wget h ...
- 帝国cms教程父栏目和子栏目都能在当前栏目高亮
首先在/e/class/userfun.php这个文件里面加上下面代码.上面父栏目的,下面子栏目的.红色代表css样式.自定义吧 function currentPage($classid,$this ...
- centos系统安装后无法稳定连接wifi的解决方法
在安装双系统的时候遇到的问题,虽然不知道原理,但是弄好能用就可以,这类bug太邪恶了 wifi不能用的情况: 先查看wifi状态: rfkill list all 0: hci0: Bluetooth ...
- ruby基础知识之 class&module
以下分别介绍了class方法和module方法,还有最简单的def方法. 其中module和class的区别下面会说,这里首先声明,def定义的方法,需要定义对象后才能调用,而class和module ...