标准库类型(一)

--命名空间using与string类型

引:

标准库类型是语言组成部分中更主要的哪些数据类型(如:数组、指针)的抽象!

C++标准库定义的是高级的抽象数据类型:

1、高级:由于当中反映了更复杂的概念。

2、抽象:由于我们在使用时不须要关心他们是怎样表示的,我们仅仅须要知道这些抽象数据类型支持哪些操作就能够了。

正文:

一、命名空间的using声明

1、 using std::cin;

::运算符的作用含义是右操作数的名字能够在左操作数的作用域中找到。

格式:

  1. using namespace::name;
  2. //一旦使用了using声明,我们就能够直接引用名字,而不须要再引用该名字的命名空间!

using namespace::name;
//一旦使用了using声明,我们就能够直接引用名字,而不须要再引用该名字的命名空间!

演示样例:

  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. int main()
  6. {
  7. cout << "Enter two numbers:" << endl;
  8. int v1, v2;
  9. cin >> v1 >> v2;
  10. cout << "The sum of " << v1
  11. << " and " << v2
  12. << " is " << v1 + v2 << endl;
  13. return 0;
  14. }
#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++标准库将负责管理与存储字符相关的内存。以及提供各种实用的操作。

  1. #include <string>
  2. using std::string;
#include <string>
using std::string;

1、string对象的定义和初始化

  1. //四种定义及初始化方式
  2. string s1;
  3. string s2(s1);
  4. string s3("value");
  5. string s4(n,'c');
//四种定义及初始化方式
string s1;
string s2(s1);
string s3("value");
string s4(n,'c');

2、string与字符串字面值的异同

1)都是以'\0'结尾

  1. string s1("value");
  2. for (string::size_type i = 0;s1[i] != '\0'; ++i)
  3. cout << s1[i] << ' ';
  4. cout << endl;
	string s1("value");
for (string::size_type i = 0;s1[i] != '\0'; ++i)
cout << s1[i] << ' ';
cout << endl;

2)字符串字面值与string根本就不是一个类型!

  1. string SI = "ABC";
  2. char SII[] = "CDE";
  3. string tmp = SI;
  4. SI = string(SII);
  5. strcpy(SII,tmp.c_str());
  6. cout << SI << endl;
  7. 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对象中!

  1. //P72 习题3.5
  2. int main()
  3. {
  4. string line;
  5. while (getline(cin,line))
  6. {
  7. cout << line << endl;
  8. }
  9. return 0;
  10. }
//P72 习题3.5
int main()
{
string line;
while (getline(cin,line))
{
cout << line << endl;
} return 0;
}
  1. //3.5(2)
  2. int main()
  3. {
  4. string word;
  5. while (cin >> word)
  6. {
  7. cout << word << endl;
  8. }
  9. return 0;
  10. }
//3.5(2)
int main()
{
string word;
while (cin >> word)
{
cout << word << endl;
} return 0;
}

4、string对象的操作 size与empty

  1. //例程1
  2. string st("The expense of spirit\n");
  3. cout << "The size of " << st << "is " << st.size()
  4. << " 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;
  1. //例程2
  2. string se;
  3. if (se.empty())
  4. {
  5. cout << "The string is empty!" << endl;
  6. }
  7. else
  8. {
  9. cout << "The size of " << se << "is " << se.size()
  10. << " characters, including the newline" << endl;
  11. }
//例程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对象的长度。

  1. string str = "Hello World";
  2. string::size_type length = str.size();
    string str = "Hello World";
string::size_type length = str.size();

6、string关系操作符

==,!=,<,<=,>=,>

  1. string big = "big",small = "small";
  2. if (big == small)   //big <= small,big >=  small,big != small,...
  3. {
  4. //...
  5. }
  6. else
  7. {
  8. //...
  9. }
	string big = "big",small = "small";
if (big == small) //big <= small,big >= small,big != small,...
{
//...
}
else
{
//...
}

7、string对象的赋值

  1. string s1,s2 = “value”;
  2. s1 = s2;
string s1,s2 = “value”;
s1 = s2;

赋值操作须要做一些操作:它必须把s1占用的相关内存释放掉。然后再分配给s1足够存放s2副本的内存空间,最后把s2中全部的字符都拷贝到新分配的内存中!

8、两个string对象相加

  1. string s1("hello, ");
  2. string s2("world\n");
  3. string s3 = s1 + s2;
  4. cout << s3 << endl;
  5. string s4 = s2 + s1;
  6. cout << s4 << endl;
  7. string s5 = s3 + "! " + "I`m a programmer!";
  8. 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

  1. string str ("Some thing!");
  2. for (string::size_type i = 0; i != str.size(); ++i)
  3. {
  4. cout << str[i];
  5. }
  6. cout << endl;
	string str ("Some thing!");
for (string::size_type i = 0; i != str.size(); ++i)
{
cout << str[i];
}
cout << endl;

10、string对象下标能够用作左值

  1. string str ("Some thing!");
  2. for (string::size_type i = 0; i != str.size(); ++i)
  3. {
  4. str[i] = '*';
  5. }
  6. 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...

  1. //P78 习题3.7
  2. //(1)
  3. int main()
  4. {
  5. string s1,s2;
  6. cin >> s1 >> s2;
  7. if (s1 > s2)
  8. {
  9. cout << s1 << " is bigger!" << endl;
  10. }
  11. else if (s1 < s2)
  12. {
  13. cout << s2 << " is bigger!" << endl;
  14. }
  15. else
  16. {
  17. cout << s1 << " is equal to " << s2 << endl;
  18. }
  19. return 0;
  20. }
//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;
}
  1. //(2)
  2. int main()
  3. {
  4. string s1,s2;
  5. cin >> s1 >> s2;
  6. if (s1.size() > s2.size())
  7. {
  8. cout << s1 << " is longer!" << endl;
  9. }
  10. else if (s1.size() < s2.size())
  11. {
  12. cout << s2 << " is longer!" << endl;
  13. }
  14. else if (s1.empty() && s2.empty())
  15. {
  16. cout << s1 << " and " << s2 << " is empty!" << endl;
  17. }
  18. else
  19. {
  20. cout << s1 << " is equal to " << s2 << endl;
  21. }
  22. return 0;
  23. }
//(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;
}
  1. //习题 3.8
  2. //(1)
  3. int main()
  4. {
  5. string sub,str;
  6. while (cin >> sub)
  7. {
  8. str += sub;
  9. }
  10. cout << str << endl;
  11. return 0;
  12. }
//习题 3.8
//(1)
int main()
{
string sub,str;
while (cin >> sub)
{
str += sub;
}
cout << str << endl;
return 0;
}
  1. //(2)
  2. int main()
  3. {
  4. string sub,str;
  5. cin >> sub;
  6. str = sub;
  7. while (cin >> sub)
  8. {
  9. str += ' ';
  10. str += sub;
  11. }
  12. cout << str << endl;
  13. return 0;
  14. }
//(2)
int main()
{
string sub,str;
cin >> sub;
str = sub;
while (cin >> sub)
{
str += ' ';
str += sub;
}
cout << str << endl;
return 0;
}
  1. //习题 3.9 以下的程序是否合法。合法的话会输出什么值?
  2. int main()
  3. {
  4. string s;
  5. cout << s[0] << endl;
  6. s[1] = '*';
  7. cout << s[1] << endl;
  8. return 0;
  9. }

C++ Primer 学习笔记_6_标准库类型 -- 命名空间using与string类型的更多相关文章

  1. C++ Primer学习笔记2--c++标准库中的 vector、string 和 bitset 类型

    一.string    #include <string>  using std::string    初始化函数:    string s1;        默认构造函数 s1 为空串 ...

  2. C++ primer读书笔记 chapter3 标准库类型

    除第二章介绍的是C++的基本类型,本章将大致介绍一下C++定义的内容丰富的抽象数据库类型标准库.着重介绍一下sting.vector和bitset. 3.2标准库string类型 1.string类型 ...

  3. C++ Primer 学习笔记_63_重载运算符和转换 --转换和类类型【上】

    重载运算符和转换 --转换与类类型[上] 引言: 在前面我们提到过:能够用一个实參调用的位 unsignedchar 相同范围的值,即:0到255. 这个类能够捕获下溢和上溢错误,因此使用起来比内置u ...

  4. js学习笔记之标准库

    在全局函数中,this等于window  在函数被作为某个对象的方法调用时,this等于那个对象. 数组的函数: 检测:Array.isArray() 转换:toString(),toLocalStr ...

  5. Python 3 学习笔记之——标准库概述

    1. 操作系统接口 os 模块提供了一些与操作系统相关联的函数. >>> os.getcwd() # 获取当前工作目录 '/home/senius' >>> os. ...

  6. C++ primer学习笔记_6_函数---函数定义、参数传递

    1. 习题参考: 6.14 举一个形参应该是引用类型的例子,再举一个形参不能是引用类型的例子. 答: 形参使用引用类型的情况:(1)避免拷贝传递大对象的时候,这里的string对象s:(2)当需要从函 ...

  7. C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)

    STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...

  8. C++primer第三章标准库类型

    除第二章介绍的基本数据类型外,C++ 还定义了一个内容丰富的抽象数据类型标准库. 本章将介绍标准库中的 vector.string 和 bitset 类型. string 类型支持长度可变的字符串 v ...

  9. C++ Primer学习笔记(三) C++中函数是一种类型!!!

    C++中函数是一种类型!C++中函数是一种类型!C++中函数是一种类型! 函数名就是变量!函数名就是变量!函数名就是变量! (---20160618最新消息,函数名不是变量名...囧) (---201 ...

随机推荐

  1. 运行Android程序出错:The connection to adb is down, and a severe error has occured

    调试Android程序时候,报错如下: [2013-02-21 15:41:06 - MainActivity] ------------------------------[2013-02-21 1 ...

  2. sqlserver的left join优化

    MS sqlserver 对4张表进行left join join字段是varchar类型长度20,也都建了索引,但是光查一个count(Id) 耗时就超过了8秒,数据量只有100多万条,该怎么优化呢 ...

  3. xampp下bugfree部署

    以Bugfree3.0.4为例,讲解如何搭建LAMP架构的Web服务器. Bugfree是一个XAMPP架构的网站,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的搭建XAMP ...

  4. 阿里面试题:说说HashMap的扩容过程?

    这是一道阿里的面试题,考察你对HashMap源码的了解情况,废话不多说,咱们就直接上源码吧! jdk 1.7 源码 void resize(int newCapacity) { Entry[] old ...

  5. zabbix基于LNMP安装

    安装依赖 yum install pcre* #为了支持rewrite功能 yum install openssl openssl-devel yum install gcc make gd-deve ...

  6. spring-boot项目热部署以及spring-devtools导致同类不能转换

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  7. Python ping 模块

    使用socket模块也可以获得域名对应的ip,参考:https://blog.csdn.net/c465869935/article/details/50850598 print socket.get ...

  8. 2017.8.2 Noip2018模拟测试赛(十八)

     日期: 八月二日  总分: 300分  难度: 提高 ~ 省选  得分: 40分(又炸蛋了!!) 题目列表: T1:分手是祝愿 T2:残缺的字符串 T3:树点涂色 赛后心得: 哎,T1求期望,放弃. ...

  9. 【CF1015B】Obtaining the String(模拟)

    题意:给定两个字符串,每次可以交换相邻两个字符,给出任意一组交换次数小于1e4的方案使得a串成为b串,输出交换的次数与位置,无解输出-1 n<=50 思路:每次找到第一个不相同的字符,从后面找到 ...

  10. Javascript实现页面滚动时导航智能定位

    遇到的问题: 在做官网的时候,需要滚动定位的区块的图片不确定,无法确定用户浏览区域对应的模块导航 之前的解决方案是: 通过定位滚动条的位置来判断用户浏览区域对应的模块导航,这种方法的弊端是,区块的高度 ...