C++ Primer 学习笔记_6_标准库类型 -- 命名空间using与string类型
标准库类型(一)
--命名空间using与string类型
引:
标准库类型是语言组成部分中更主要的哪些数据类型(如:数组、指针)的抽象!
C++标准库定义的是高级的抽象数据类型:
1、高级:由于当中反映了更复杂的概念。
2、抽象:由于我们在使用时不须要关心他们是怎样表示的,我们仅仅须要知道这些抽象数据类型支持哪些操作就能够了。
正文:
一、命名空间的using声明
1、 using std::cin;
::运算符的作用含义是右操作数的名字能够在左操作数的作用域中找到。
格式:
- using namespace::name;
- //一旦使用了using声明,我们就能够直接引用名字,而不须要再引用该名字的命名空间!
using namespace::name;
//一旦使用了using声明,我们就能够直接引用名字,而不须要再引用该名字的命名空间!
演示样例:
- #include <iostream>
- using std::cin;
- using std::cout;
- using std::endl;
- int main()
- {
- cout << "Enter two numbers:" << endl;
- int v1, v2;
- cin >> v1 >> v2;
- cout << "The sum of " << v1
- << " and " << v2
- << " is " << v1 + v2 << endl;
- return 0;
- }
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << "Enter two numbers:" << endl; int v1, v2;
cin >> v1 >> v2; cout << "The sum of " << v1
<< " and " << v2
<< " is " << v1 + v2 << endl; return 0;
}
2、在一种情况下。必须总是使用全然限定的标准库名字:在头文件里。
通常,头文件里应该仅仅定义确实必要的东西。请养成这个好习惯!
二、标准库string类型
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存。以及提供各种实用的操作。
- #include <string>
- using std::string;
#include <string>
using std::string;
1、string对象的定义和初始化
- //四种定义及初始化方式
- string s1;
- string s2(s1);
- string s3("value");
- string s4(n,'c');
//四种定义及初始化方式
string s1;
string s2(s1);
string s3("value");
string s4(n,'c');
2、string与字符串字面值的异同
1)都是以'\0'结尾
- string s1("value");
- for (string::size_type i = 0;s1[i] != '\0'; ++i)
- cout << s1[i] << ' ';
- cout << endl;
string s1("value");
for (string::size_type i = 0;s1[i] != '\0'; ++i)
cout << s1[i] << ' ';
cout << endl;
2)字符串字面值与string根本就不是一个类型!
- string SI = "ABC";
- char SII[] = "CDE";
- string tmp = SI;
- SI = string(SII);
- strcpy(SII,tmp.c_str());
- cout << SI << endl;
- cout << SII << endl;
string SI = "ABC";
char SII[] = "CDE";
string tmp = SI;
SI = string(SII);
strcpy(SII,tmp.c_str());
cout << SI << endl;
cout << SII << endl;
3、getline读取整行文本
1)、getline函数从输入流的下一行读取。并保存读取内容到string中,可是不包含换行符!
2)、getline函数将stream參数作为返回值!
3)、因为getline在返回时丢弃换行符,所以换行符将不会保存在string对象中!
- //P72 习题3.5
- int main()
- {
- string line;
- while (getline(cin,line))
- {
- cout << line << endl;
- }
- return 0;
- }
//P72 习题3.5
int main()
{
string line;
while (getline(cin,line))
{
cout << line << endl;
} return 0;
}
- //3.5(2)
- int main()
- {
- string word;
- while (cin >> word)
- {
- cout << word << endl;
- }
- return 0;
- }
//3.5(2)
int main()
{
string word;
while (cin >> word)
{
cout << word << endl;
} return 0;
}
4、string对象的操作 size与empty
- //例程1
- string st("The expense of spirit\n");
- cout << "The size of " << st << "is " << st.size()
- << " characters, including the newline" << endl;
//例程1
string st("The expense of spirit\n");
cout << "The size of " << st << "is " << st.size()
<< " characters, including the newline" << endl;
- //例程2
- string se;
- if (se.empty())
- {
- cout << "The string is empty!" << endl;
- }
- else
- {
- cout << "The size of " << se << "is " << se.size()
- << " characters, including the newline" << endl;
- }
//例程2
string se;
if (se.empty())
{
cout << "The string is empty!" << endl;
}
else
{
cout << "The size of " << se << "is " << se.size()
<< " characters, including the newline" << endl;
}
5、string::size_type类型
string类类型和更多库类型都定义了一些配套类型,通过这些配套类型。库类型的使用就能与机器无关(machine-independent)。
string::size_type类型可以保证足够大到可以存储随意string对象的长度。
- string str = "Hello World";
- string::size_type length = str.size();
string str = "Hello World";
string::size_type length = str.size();
6、string关系操作符
==,!=,<,<=,>=,>
- string big = "big",small = "small";
- if (big == small) //big <= small,big >= small,big != small,...
- {
- //...
- }
- else
- {
- //...
- }
string big = "big",small = "small";
if (big == small) //big <= small,big >= small,big != small,...
{
//...
}
else
{
//...
}
7、string对象的赋值
- string s1,s2 = “value”;
- s1 = s2;
string s1,s2 = “value”;
s1 = s2;
赋值操作须要做一些操作:它必须把s1占用的相关内存释放掉。然后再分配给s1足够存放s2副本的内存空间,最后把s2中全部的字符都拷贝到新分配的内存中!
8、两个string对象相加
- string s1("hello, ");
- string s2("world\n");
- string s3 = s1 + s2;
- cout << s3 << endl;
- string s4 = s2 + s1;
- cout << s4 << endl;
- string s5 = s3 + "! " + "I`m a programmer!";
- cout << s5 << endl;
string s1("hello, ");
string s2("world\n");
string s3 = s1 + s2;
cout << s3 << endl;
string s4 = s2 + s1;
cout << s4 << endl;
string s5 = s3 + "! " + "I`m a programmer!";
cout << s5 << endl;
9、string对象str下标的取值范围:0~str.size()-1
- string str ("Some thing!");
- for (string::size_type i = 0; i != str.size(); ++i)
- {
- cout << str[i];
- }
- cout << endl;
string str ("Some thing!");
for (string::size_type i = 0; i != str.size(); ++i)
{
cout << str[i];
}
cout << endl;
10、string对象下标能够用作左值
- string str ("Some thing!");
- for (string::size_type i = 0; i != str.size(); ++i)
- {
- str[i] = '*';
- }
- cout << str << endl;
string str ("Some thing!");
for (string::size_type i = 0; i != str.size(); ++i)
{
str[i] = '*';
}
cout << str << endl;
11、string对象中的字符处理函数
isalnum,isalpha,iscntrl,isdigit,isgraph,islower,isprint,ispunct,isspace,isupper,isxdigit,tolower,toupper...
- //P78 习题3.7
- //(1)
- int main()
- {
- string s1,s2;
- cin >> s1 >> s2;
- if (s1 > s2)
- {
- cout << s1 << " is bigger!" << endl;
- }
- else if (s1 < s2)
- {
- cout << s2 << " is bigger!" << endl;
- }
- else
- {
- cout << s1 << " is equal to " << s2 << endl;
- }
- return 0;
- }
//P78 习题3.7
//(1)
int main()
{
string s1,s2;
cin >> s1 >> s2;
if (s1 > s2)
{
cout << s1 << " is bigger!" << endl;
}
else if (s1 < s2)
{
cout << s2 << " is bigger!" << endl;
}
else
{
cout << s1 << " is equal to " << s2 << endl;
}
return 0;
}
- //(2)
- int main()
- {
- string s1,s2;
- cin >> s1 >> s2;
- if (s1.size() > s2.size())
- {
- cout << s1 << " is longer!" << endl;
- }
- else if (s1.size() < s2.size())
- {
- cout << s2 << " is longer!" << endl;
- }
- else if (s1.empty() && s2.empty())
- {
- cout << s1 << " and " << s2 << " is empty!" << endl;
- }
- else
- {
- cout << s1 << " is equal to " << s2 << endl;
- }
- return 0;
- }
//(2)
int main()
{
string s1,s2;
cin >> s1 >> s2;
if (s1.size() > s2.size())
{
cout << s1 << " is longer!" << endl;
}
else if (s1.size() < s2.size())
{
cout << s2 << " is longer!" << endl;
}
else if (s1.empty() && s2.empty())
{
cout << s1 << " and " << s2 << " is empty!" << endl;
}
else
{
cout << s1 << " is equal to " << s2 << endl;
}
return 0;
}
- //习题 3.8
- //(1)
- int main()
- {
- string sub,str;
- while (cin >> sub)
- {
- str += sub;
- }
- cout << str << endl;
- return 0;
- }
//习题 3.8
//(1)
int main()
{
string sub,str;
while (cin >> sub)
{
str += sub;
}
cout << str << endl;
return 0;
}
- //(2)
- int main()
- {
- string sub,str;
- cin >> sub;
- str = sub;
- while (cin >> sub)
- {
- str += ' ';
- str += sub;
- }
- cout << str << endl;
- return 0;
- }
//(2)
int main()
{
string sub,str;
cin >> sub;
str = sub;
while (cin >> sub)
{
str += ' ';
str += sub;
}
cout << str << endl;
return 0;
}
- //习题 3.9 以下的程序是否合法。合法的话会输出什么值?
- int main()
- {
- string s;
- cout << s[0] << endl;
- s[1] = '*';
- cout << s[1] << endl;
- return 0;
- }
C++ Primer 学习笔记_6_标准库类型 -- 命名空间using与string类型的更多相关文章
- C++ Primer学习笔记2--c++标准库中的 vector、string 和 bitset 类型
一.string #include <string> using std::string 初始化函数: string s1; 默认构造函数 s1 为空串 ...
- C++ primer读书笔记 chapter3 标准库类型
除第二章介绍的是C++的基本类型,本章将大致介绍一下C++定义的内容丰富的抽象数据库类型标准库.着重介绍一下sting.vector和bitset. 3.2标准库string类型 1.string类型 ...
- C++ Primer 学习笔记_63_重载运算符和转换 --转换和类类型【上】
重载运算符和转换 --转换与类类型[上] 引言: 在前面我们提到过:能够用一个实參调用的位 unsignedchar 相同范围的值,即:0到255. 这个类能够捕获下溢和上溢错误,因此使用起来比内置u ...
- js学习笔记之标准库
在全局函数中,this等于window 在函数被作为某个对象的方法调用时,this等于那个对象. 数组的函数: 检测:Array.isArray() 转换:toString(),toLocalStr ...
- Python 3 学习笔记之——标准库概述
1. 操作系统接口 os 模块提供了一些与操作系统相关联的函数. >>> os.getcwd() # 获取当前工作目录 '/home/senius' >>> os. ...
- C++ primer学习笔记_6_函数---函数定义、参数传递
1. 习题参考: 6.14 举一个形参应该是引用类型的例子,再举一个形参不能是引用类型的例子. 答: 形参使用引用类型的情况:(1)避免拷贝传递大对象的时候,这里的string对象s:(2)当需要从函 ...
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...
- C++primer第三章标准库类型
除第二章介绍的基本数据类型外,C++ 还定义了一个内容丰富的抽象数据类型标准库. 本章将介绍标准库中的 vector.string 和 bitset 类型. string 类型支持长度可变的字符串 v ...
- C++ Primer学习笔记(三) C++中函数是一种类型!!!
C++中函数是一种类型!C++中函数是一种类型!C++中函数是一种类型! 函数名就是变量!函数名就是变量!函数名就是变量! (---20160618最新消息,函数名不是变量名...囧) (---201 ...
随机推荐
- 【转载】logistic回归
原文地址:https://www.cnblogs.com/zichun-zeng/p/3824745.html 1. logistic回归与一般线性回归模型的区别: (1) 线性回归的结果变量 ...
- April Fools Contest 2018
这个比赛不正经,但是我可以一本正经的写代码啊 A. Quirky Quantifiers time limit per test 2 seconds memory limit per test 64 ...
- php hash防止表单
<?php /** * Created by PhpStorm. * User: brady * Desc: * Date: 2017/7/12 * Time: 15:01 */class te ...
- 最短路---dijsktra--邻接矩阵
; ; //点的个数 int dist[MAXNUM]; int prev[MAXNUM]; int A[MAXUNM][MAXNUM]; void Dijkstra(int v0) { bool S ...
- 【基础操作】2-sat
$2-sat$ 是一个很不怎么考的内容($NOI2017$ 除外) 例题
- Linux System Programming 学习笔记(四) 高级I/O
1. Scatter/Gather I/O a single system call to read or write data between single data stream and mu ...
- 使用 new Q_max_capacity 參數,同樣 loading 下,粗估耗電量(UI 上的 %)。
Precondition : 除了 Q_max 外,其它參數皆同. old Q_max_capacity : 1500 mAh new Q_max_capacity : 2200 mAh 有一個 lo ...
- 快充 IC BQ25896 的 常用參數
一: POWER-PATH MANAGEMENT (有接 adapter) 1:Vbat > Vsysmin,Isys = 0A, BATFET disable Vsys = Vbat + 50 ...
- HDU 4405: Aeroplane chess
类型:概率DP 题意:一条直线下飞行棋,色子六个面等概率.同时存在一些飞机航线,到了某个点可以直接飞到后面的另一个点,可以连飞,保证一个点至多一条航线.求到达或者超过终点 所需要 掷色子的期望次数. ...
- GNOME 3.x下安装配置小企鹅输入法框架及SunPinYin插件
fcitx 小企鹅输入法框架已经越来越成熟,并且具备极高的性能,配合 Sun PinYin 智能输入法就和 Windows 下的搜狗百度等输入法几乎无二了.事实上,现在Linux版本的搜狗输入法正是基 ...