[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 ...
随机推荐
- ios常用数据库、完美无缺
直接copy过去就能用,我们不用再去造轮子,现在的xocod9.4更加人性化了,不用再添加依赖库,这点苹果你让我开始喜欢了,哈哈. 需要这兄弟拉进去的哈 下班标的1,2,3,4就是就截图的4个文件,没 ...
- [videos系列]日本的videos视频让男人产生了哪些误解?
转载自:[videos系列]日本的videos视频让男人产生了哪些误解? 日本的videos视频是每个男人成长过程中都会看的启蒙教育片,也是男人在成年后调剂生活的必需品,但是由于影视作品是艺术的,是属 ...
- 并发、并行与C++多线程——基础一
1.什么是并发? 并发指的是两个或多个独立的活动在同一时段内发生.生活中并发的例子并不少,例如在跑步的时候你可能同时在听音乐:在看电脑显示器的同时你的手指在敲击键盘.这时我们称我们大脑并发地处理这些事 ...
- LogViewer超大文本浏览工具
官方下载 LogViewer 是一款简单好用的log日志文件查看工具.您想要查看log日志吗?那么不妨来看看这款LogViewer .该款工具可以在短短数秒内打开上G的LOG文件,支持高亮某行文字(例 ...
- Python模拟校园网登录
最近忙着实验室的项目,学习的时间相对较少.前一段时间刚开始接触python时,依葫芦画瓢照着写了一个爬虫,爬取了某个网站的图片.当看到一张张图片自动出现在电脑屏幕上时,有些小小成就感.我想大多数人开始 ...
- 增强for循环和迭代器
package example6; import java.util.ArrayList;import java.util.Iterator;import java.util.List; class ...
- day 12 生成器和生成器函数以及各种推导式
一.生成器 本质就是迭代器. 我们可以直接执⾏__next__()来执⾏ 以下⽣成器 一个一个的创建对象 创建生成器的方式: 1.生成器函数 2.通过生成器 表达式来获取生成器 3.类型转换(看 ...
- vowels_单元音
vowels(美式): 单元音: [i]:需要用劲喊出类似于“yi”的四声,费力咧开嘴,单词eat.need.thief.meet. [?]:卷舌音,单词bird.her.worry.certain. ...
- MapWindow记录
增加MapWinGIS的新功能,编译完MapWinGIS,可以生成Debug和Release版本的x64和Win32四种版本, 自己基于c#的Mapwindow如果要用到新添加的功能,此时就得重新注册 ...
- Java:xxx is not an enclosing class
1. 错误原因 该错误一般出现在对内部类进行实例化时,例如 public class A{ public class B{ } } 此时B是A的内部类,如果我们要使用如下语句实例化一个B类的对象: A ...