ITeye博客

c++ primer 3 标准库类型

oolala · 2013-12-27 16:04 · c++ · 编程语言 · Markdown

c++标准库定义了高级的抽象数据类型(abstract data type)

 

二种最重要的标准库类型string vector

 

1、命令空间的using声明

using声明可以在不需要加前缀namespace_name::的情况下访问命令空间中的名字

using namespace::name

 

使用了using声明,我们就可以直接引用名字,而不需要再引用该名字的命令空间

 

一个using声明一次只能作用于一个命名空间成员,using声明可用来明确指定在程序中用到的命令空间中的名字,如果希望使用std中的几个名字,

则必须为要用到的每个名字都提供一个using声明。

 

 

2、标准库stirng类型

#include <string>

using std::string

 

string对象的定义和初始化

string s1;

string s2(s1);

string s3("value");

string s4(n,'c');

 

String 对象的读写

#include <iostream>

#include <string>

 

using namespace std;

using std::string;

 

int main(void)

{

cout << "using" << endl;

string in;

cin >> in;

 

cout << "输入的字符" + in << endl;

 

string s1("admin");

cout << s1 << endl;

return 0;

}

 

读入未知数目的string对象

#include <iostream>

#include <string>

 

using namespace std;

using std::string;

 

/************************************************************************/

/* 读入未知数目的string 对象    */

/************************************************************************/

int main(void)

{

string word;

while(cin >> word) {

cout << word << endl;

}

return 0;

}

 

用getline读取整行文本

#include <iostream>

#include <string>

 

using namespace std;

using std::string;

 

int main(void)

{

string word;

while(getline(cin,word))

{

cout << word << endl;

}

return 0;

}

 

 

 

string对象的操作

s.empty

s.size()

s[n]

s1 + s2

s1 = s2

!= ,< ,<= ,>=

 

 

string empty size

#include <iostream>

#include <string>

 

using namespace std;

using std::string;

 

int main()

{

string s1("the expense of spring");

cout << "the size of" << s1 << "is" << s1.size() << endl;

cout << s1.empty() << endl;

return 0;

}

 

 

string 关系操作符

 

从string对象获取字符

 

#include <iostream>

#include <string>

 

using namespace std;

using std::string;

 

int main(void)

{

string str("stradmin");

for(int i=0;i<str.size();i++)

{

cout << str[i] <<endl;

str[i] = '*';

}

cout << str << endl;

return 0;

}

 

 


3.3标准vector类型

 

创建确定个数元素

#include <string>

#include <vector>

#include <iostream>

 

using namespace std;

using std::string;

using std::vector;

 

int main(void)

{

vector<int> ivec1;

vector<int> ivec2(ivec1);

vector<string> ivec3("admin");

 

vector<int> ivec4(10,-1);

vector<string> ivec5(10,"hi");

}

 

 

值初始化

如果没有指定元素初始化式,那么标准库将自行提供一个元素初始值进行值初始化。

 

vector对象的操作

v.empty()

v.size()

v.push_back(t)

v[n]

v1 = v2

v1 == v2

!= <, <=,>,>=

 

#include <iostream>

#include <vector>

#include <string>

 

using namespace std;

using std::string;

using std::vector;

 

int main()

{

string word;

vector<string> text;

 

text.push_back("admin");

text.push_back("root");

text.push_back("public");

 

for(vector<int>::size_type i = 0;i<text.size();i++)

{

cout<< text[i] << endl;

}

return 0;

}

 

 


3.4迭代器

除了使用下标来访问vector对象的元素外,标准库还提供了另一种访问元素的方法:迭代器

现代C++程序更倾向于使用迭代器而不是下标操作访问容器

 

容器的iterator类型

vector<int>::iterator iter;

 

begin

vector<int>::iterator iter =  ivec.begin();

如果容器中有元素的话,begin返回的迭代器指向第一个元素

把iter初始化为由名为begin的vector操作返回的值

 

end

返回迭代器指向vector的末端元素的下一个

 

vector迭代器的自增和解引用运算

迭代器类型可使用解引用操作符(*),来访问迭代器所指向的元素

*iter = 0;   *iter 和ivec[0]就是指向同一个元素

 

 

iterator 示例:

#include <iostream>

#include <string>

#include <vector>

 

using namespace std;

using std::vector;

using std::string;

 

int main()

{

vector<string> v1;

v1.push_back("stdio");

v1.push_back("types.h");

v1.push_back("net.h");

 

for(vector<string>::iterator iter = v1.begin(); iter != v1.end(); iter++ )

{

cout << *iter << endl;

}

return 0;

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。