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

  1. c# post basic 接口

    string url = "http://xxxxxxxxx";//地址,请反复检查地址的准确性 string usernamePassword = "username: ...

  2. 2. Retrofit2 -- Basic Authentication on Android

    2. Retrofit2 -- Basic Authentication on Android android Retrofit tutorial 整合基本的认证 Retrofit 1.9 Retro ...

  3. 在ABP中创建Person实体类

    经过之前的准备目前我们的项目,终于可以搞正式的开发工作了. 创建实体Person 在Core类库中添加Person类 /// <summary> /// 联系人 /// </summ ...

  4. java设计模式之建造者模式

    学习了设计模式,一直感觉有进步又没有进步,与同学.同事探讨了一下.变化不可能一会就可以的,需要努力坚持.不管进步大小,也不管是否进步,做到勿忘初心,做自己喜欢的事情就好.还有几个设计模式一直没有写,原 ...

  5. go access database demo

    package main import ( "database/sql" "fmt" _ "github.com/lib/pq" " ...

  6. go liteIDE

    go  liteIDE 1 COMM FILE package pricetable import ( "fmt" "math" "os" ...

  7. http协议Authorization认证方式在Android开发中的使用

    我们都知道,http协议是一种无状态协议,在Web开发中,由于Session和Cookie的使用,使得服务端可以知道客户端的连接状态,即用户只需要在浏览器上登录一次,只要浏览器没有关闭,后续所有的请求 ...

  8. 用 for/in 在 Java 5.0 中增强循环

    这个方便的构造提供了什么?什么时候适用于代码? Brett McLaughlin (brett@newInstance.com), 作者/编辑, O'Reilly Media, Inc. 简介: fo ...

  9. 1111 WordReplace

    #include<iostream> #include<string> using namespace std; int main() { string sa,sb,s; wh ...

随机推荐

  1. GPUImage源码解读之GPUImageFramebufferCache

    简介 由于GPUImage添加滤镜可以形成一个FilterChain,因此,在渲染的过程中,可能会需要很多个FrameBuffer,但是正如上文所说,每生成一个FrameBuffer都需要占用一定的内 ...

  2. 网站程序application, session, cookie区别的一点小总结

    cookie:是存放于本地的文件,可以设置期限,一般来讲是比较小文本或键值对等,主要用于记录用户浏览信息,账号密码等等.可以让人们的登录变得方便,因为有了cookie,在一段时间内就可以自动登录以前所 ...

  3. js浮点型,整型操作方法汇总(进行中)

    浮点数操作方法如下: 1. Math.ceil()用作向上取整.(ceil 天花板) 2. Math.floor()用作向下取整. (floor 地板) (js 中取整底层原理是位运算的取反~运算,运 ...

  4. 浅析Vue原理(部分源码解析)

    响应式 Object.defineProperty Object.defineProperty(obj, prop, descriptor) // 对象.属性.描述符 Object.definePro ...

  5. MySQL 5.7修改root密码的4种方法

            sometimes we will forget our password of root in MySQL DB server.so,there're several methods ...

  6. linux 学习第七天

    一.bash 使用(for循环.while循环) 1.1.批量添加用户 1.2.查看用户是否存在 A.cut -d : -f 1 /etc/passwd B.id dream  (id 用户名称) C ...

  7. laravel-admin 创建数据库并生成控制器

    以user表为例 1. 生成迁移:php artisan make:migration create_users_table 在 database/migration 中生成迁移文件,可对迁移文件进行 ...

  8. 内网环境下为Elasticsearch 5.0.2 添加head服务

    背景: 本项目的服务器是内网环境,没有网络,因此需要在离线的环境中,安装head服务. 需要用到的安装包有: node的安装包 elasticsearch的head插件源码 说明:此次只讲述为elas ...

  9. CAT 安装运行配置教程

    CAT安装教程 首先安装mysql数据库,具体步骤参阅<mysql免安装教程>--http://www.cnblogs.com/halberts/p/8723938.html 下载CAT代 ...

  10. 树莓派驱动DHT22

    树莓派-DHT22测量湿度 一般的温湿度传感器有dht11和dht22,dht11比较便宜,dht22比dht11贵好几倍,自然测量的准确度肯定是dht22高一些.追求更高精准度的可以使用SHT1x. ...