1.using的用法及注意事项
<1> using作用 : 想使用命名空间里的成员时,进行using声明后,
在使用命名空间里的成员时就不需要专门的前缀了(如std::)
<2>using的声明格式:using namespace::name; (eg:using std::cin;)
<3>注意事项:每个名字都需要独立的using声明
头文件不应该包含using声明(多个文件都有同一using声明,会造成重复声明)
另外:如果想要使用命名空间里的所有名字可直接声明命名空间如:using namespace::std;
2.标准库类型string
<1>基本介绍:表示可变长的字符序列,需要包含头文件string
<2>定义及初始化string 对象
/*string的初始化
直接初始化
拷贝初始化
用cin进行赋值时,string不允许有空格
eg:输入“ hello world ”实际存入的是“hello”*/
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout; using std::endl;
int main(void)
{
string s1(10,'c');//直接初始化
string s2 = s1; //拷贝初始化
string s3; //用作cin初始化
cin >> s3>>s2;
cout << "string s3 is " << s3 << " string s2 is " << s2 << endl; //cin输入流以空格隔开
getline(cin, s1);
cout << "string s1 is " << s3 << endl;
system("pause");
return 0;
}
输入 hello world hello world
输出string s3 is hello strings2 is world string s1 is hello world
(需要注意的是1.getline 第一个参数是输入流,第二个参数是string对象
2.cin流之间用空格隔开,而getline会一直读取到回车才结束)
<3>string的运用
(1)empty和size操作
/*string的empty、size操作
empty:判断一个string对象是否为空,是空的返回1,否则返回0
szie :返回string对象的长度
用例:向string对象写入内容并且输出其内容和大小*/
#include <iostream>
#include <string>
using namespace::std;
int main(void)
{
string s1;
while (getline(cin, s1) && s1.empty());//当输入不为空才向下执行
cout << s1 << endl;
auto size = s1.size();//size的类型是string::size_type
cout << size << endl;
system("pause");
return 0;
}
输入hello world
输出hello world
11
(2)string的比较操作
/*string的比较运算
1、 ==和!= 常用
2、 >=、>、<、<=比较字符不常用
比较规则 1、如果两个string对象,a包含于b(eg:a = "hello",b = "hello world")
那么a < b
2、如果连个string对象某些对应位置上的字符不一样,
比较结果是两个string对象中,第一个相异字符的比较结果*/
#include <iostream>
#include <string>
using namespace::std;
int main(void)
{
string s1;
string s2;
getline(cin, s1);
getline(cin, s2);
if (s1 > s2)
cout << "s1 > s2" << endl;
else
cout << "s1 < s2" << endl;
system("pause");
return 0;
}
#endif
输入hello world <回车> hello <回车>
输出s1 > s2
输入abc <回车> abd <回车>
输出s1 < s2
(2)string的相加操作
两个string对象相加会得到一个新的string对象,其内容是把左侧的运算对象与右侧的运算对象拼接而成
string对象可以和字面值(eg:“hello”)相加。
两个字面值不可以直接相加
/*string的+运算*/
#include <iostream>
#include <string>
using namespace::std;
int main(void)
{
string s1(3, 'a');
string s2(3, 'b');
s2 = s2 + s1; //相加相当于字符串从左到右展开
cout << "s2 + s1 :" << s2 << endl;
s2 = "bbb";
s2 = s1 + s2;
cout << "s1 + s2 :" << s2 << endl;
s2 = s1 + "ccc";
cout << "s1 + ccc :" << s2 << endl;
//s2 = "a" + "b"; //错误,不可以是两个字面量相加
system("pause");
return 0;
}
(3)处理string对象中的字符
相应的好用的头文件是cctype,里面有许多关于字符串的操作函数
范围for 语句
语法 for(declaration : esxpression)
statement
expression是一个对象,用于表示一个序列,declaration负责定义一个变量,该变量将被用于访问序列中的基础元素,每
次迭代,declaration 部分的变量会被初始化为expression的下一个元素值。
/*处理string对象中的字符
范围for的用法
加&,可以改变字符串中的字符*/
#include <iostream>
#include <string>
#include <cctype>
using namespace::std;
int main(void)
{
string s1(3, 'a');
string s2(3, 'b');
for (auto c : "we are friend") //c的类型和每次被初始化的类型有关,这里是char
cout << c << endl;
for (auto &c : s1) //加上&,表示引用了每次初始化给它的变量,因此可以该百年字符串里的字符
c = toupper(c);
cout << "s1 is " << s1 << endl;
system("pause");
return 0;
}
如果只访问其中一部分字符的话,只用范围是不够的,还需要使用下标运算符([ ]),下面这个示例会很好
/*处理string对象中的一部分字符
范围for的用法and下标的用法
用例:将 0-15内的十进制数转化为十六进制*/
#include <iostream>
#include <string>
#include <cctype>
using namespace::std;
int main(void)
{
const string s1 = {"0123456789ABCDEF"};
cout << "please Enter a series of number between 0 and 15" << endl;
string result; //保存转化后的结果
string::size_type n; //保存从输入流读取的数
while (cin >> n) //输出完成后请输入end
if (n < s1.size())
{
result += s1[n];
result += " ";
}
cout << result << endl;
system("pause");
return 0;
}
3、标准库类型vector
<1>定义 :标准库类型vector表示对象的集合,其中所有对象的类型都相同。
(注意:vector 是模板而非类型,由vector生成的类型必须包含vector中元素的类型,eg:vector<int>)
示例解析:
vector<int> sale; //1、编译器根据模板vector生成类型vector<int>
//2、sale的类型是vector<int>,sale可以保存int型的对象,类似于数组的概念
//3、可以称它为int型对象的集合
//本例sale是一个空vector,但是可以通过push_back动态添加其元素
vector<vector<int> > price; //price的元素是 vector<int>型对象 或者说vector对象
//个人比较喜欢说price是int型对象集合的集合
可以通过这个程序验证
/*标准库类型vector
vector的两种初始化
1.列表初始化 {}
2.构造vector对象 ()
*/
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace::std;
int main(void)
{
vector<string> s2 = { "a", "b","c" };//s2是string对象的集合
vector<vector<string>> file(3,"x"); //file的元素是string对象的集合
file[1] = s2;//验证执行成功
system("pause");
return 0;
}
<1>push_back操作
push_back把一个值当成vector对象的尾元素压到vector对象的尾端
#ifdef test_7
/*push_back操作*/
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace::std;
int main(void)
{
vector<int> s2;
for (int i = 0; i != 100; i++)
{
s2.push_back(i);
}
system("pause");
return 0;
}
#endif
注意vector对象不可以直接输出,如果要输出s2,可以借助下标操作。eg:cout << s2[2] <<endl;
4、迭代器
<1>定义 是一种类型,用于访问容器中的元素或者元素中的移动。(和指针相差不多,提供了一种间接访问的机制)
<2>迭代器的使用
获取迭代器:有迭代器的类型同时拥有返回迭代器的成员。
begin():返回指向第一个元素的迭代器
end() :返回指向容器尾元素的下一个位置,返回的迭代器常被称为尾后迭代器。
例如:string x = {“abcde”};
/*迭代器的简单用法*/
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace::std;
int main(void)
{
string s2 = "hello world";
for (auto i = s2.begin(); i != s2.end() && !isspace(*i); i++)
{
*i = toupper(*i);
}
cout << s2 << endl;
system("pause");
return 0;
}
迭代器类型是iterator、const_iterator ,了解一下就好。
在这里提一下标准库类型vector
/*迭代器的用法
vector<string> 型对象 ,返回的是一个二级指针
vector<vector<string>> ,返回的是一个三级指针
解引用迭代器可获得迭代器所指向的对象,如果对象是类,就可能希望进一步访问它的成员
string 本质返回的是一个指针。*/
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace::std;
int main(void)
{
vector<string> s2 = { "hello world" ,"hello" ,"x"};
vector<vector<string>> file(2);
file[0] = s2;
cout << (s2.begin() + 1)->size() << endl; //返回hello_world的长度
cout << (file.begin() )->size() << endl; //返回s2的元素的个数
system("pause");
return 0;
}