您的位置:首页 > 编程语言 > C语言/C++

C++(3)标准库类型

2015-08-07 18:41 477 查看


标准库类型(一)--命名空间using与string类型

引:

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

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

1、高级:因为其中反映了更复杂的概念;

2、抽象:因为我们在使用时不需要关心他们是如何表示的,我们只需要知道这些抽象数据类型支持哪些操作就可以了。

正文:


一、命名空间的using声明

1、 using std::cin;

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

格式:

[cpp] view
plaincopy





using namespace::name;

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

示例:

[cpp] view
plaincopy





#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++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作。

[cpp] view
plaincopy





#include <string>

using std::string;

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

[cpp] view
plaincopy





//四种定义及初始化方式

string s1;

string s2(s1);

string s3("value");

string s4(n,'c');

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

1)都是以'\0'结尾

[cpp] view
plaincopy





string s1("value");

for (string::size_type i = 0;s1[i] != '\0'; ++i)

cout << s1[i] << ' ';

cout << endl;

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

[cpp] view
plaincopy





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对象中!

[cpp] view
plaincopy





//P72 习题3.5

int main()

{

string line;

while (getline(cin,line))

{

cout << line << endl;

}



return 0;

}

[cpp] view
plaincopy





//3.5(2)

int main()

{

string word;

while (cin >> word)

{

cout << word << endl;

}



return 0;

}

4、string对象的操作 size与empty

[cpp] view
plaincopy





//例程1

string st("The expense of spirit\n");

cout << "The size of " << st << "is " << st.size()

<< " characters, including the newline" << endl;

[cpp] view
plaincopy





//例程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对象的长度。

[cpp] view
plaincopy





string str = "Hello World";

string::size_type length = str.size();

6、string关系操作符

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

[cpp] view
plaincopy





string big = "big",small = "small";

if (big == small) //big <= small,big >= small,big != small,...

{

//...

}

else

{

//...

}

7、string对象的赋值

[cpp] view
plaincopy





string s1,s2 = “value”;

s1 = s2;

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

8、两个string对象相加

[cpp] view
plaincopy





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

[cpp] view
plaincopy





string str ("Some thing!");

for (string::size_type i = 0; i != str.size(); ++i)

{

cout << str[i];

}

cout << endl;

10、string对象下标可以用作左值

[cpp] view
plaincopy





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...

[cpp] view
plaincopy





//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;

}

[cpp] view
plaincopy





//(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;

}

[cpp] view
plaincopy





//习题 3.8

//(1)

int main()

{

string sub,str;

while (cin >> sub)

{

str += sub;

}

cout << str << endl;

return 0;

}

[cpp] view
plaincopy





//(2)

int main()

{

string sub,str;

cin >> sub;

str = sub;

while (cin >> sub)

{

str += ' ';

str += sub;

}

cout << str << endl;

return 0;

}

[cpp] view
plaincopy





//习题 3.9 下面的程序是否合法,合法的话会输出什么值?

int main()

{

string s;

cout << s[0] << endl;

s[1] = '*';

cout << s[1] << endl;

return 0;

}


标准库类型(二)--vector类型

引子:

vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值。和string对象一样,标准库将负责管理与存储元素相关的内存。

我们将vector称之为容器,一个容器中的所有对象都必须是同一类型的!

[cpp] view
plaincopy





#include <vector>

using std::vector;

【模板】

vector就是一个类模板,使用模板可以编写一个类定义或函数定义,而用于多个不同的数据类型!

但是,声明从类模板产生的某种类型的对象,需要提供附加信息。如:

vector并不是一种数据类型,而vector<string>,vector<int>都是数据类型!

正文:

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

[cpp] view
plaincopy





//vector的四种初始化方式

vector<T> v1;

vector<T> v2(v1);

vector<T> v3(n,i);

vector<T> v4(n);

示例:

[cpp] view
plaincopy





vector<int> ivec1;

vector<int> ivec2(ivec1);

/*

*ERROR

*vector<string> strVec(ivec2);

*/



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

vector<string> strVec(10,"HI");

2、vector对象的值初始化

1)如果vector对象保存的是内置数据类型(如:int),那么标准库将用0值创建元素初始化式。

2)如果vector保存的是含有构造函数的类类型的元素,那么标准库将用该类型的默认构造函数创建元素初始化式。

*3)如果vector保存的类类型元素没有默认构造函数,程序员就不能仅提供元素个数,还要提供初始值。

3、vector对象的动态增长

因为vector的增长效率非常高,所以,当元素值已知时,最好是通过动态的向它添加元素来让他“成长^_^”.

【P79关键概念:vector对象的动态增长非常C/Java及其他程序员一读,推荐】

[cpp] view
plaincopy





//P80 习题3.11 下面语句正确or错误?

vector<vector<int>> ivec; //在C++11中正确,在C++98/03中错误

vector< vector<int> > ivec; //正确

vector<string> svec(10,"NULL"); //正确

4、vector对象的size

成员函数size返回相应的vector类定义的size_type的值。

[cpp] view
plaincopy





vector<int>::size_type length = st.size(); //正确

vector::size_type lenth; //错误

5、push_back()操作接受一个元素值。

[cpp] view
plaincopy





int main()

{

string word;

unsigned count = 0;

vector<string> strVec;



while (cin >> word)

{

++ count;

strVec.push_back(word);

}



if (count == strVec.size())

{

cout << "Equal!" << endl;

}

//C++程序员应习惯于用 != 来限定循环的约束条件

for (vector<string>::size_type index = 0; index != strVec.size(); ++index)

{

cout << strVec[index] << endl;

}



return 0;

}

【P82关键概念:安全的泛型编程 推荐阅读!】

6、下标操作不添加元素!

[cpp] view
plaincopy





vector<int> ivec;

for (vector<int>::size_type index = 0; index != 10; ++index)

{

/*

*必须是已经存在的元素才能使用下标操作符进行索引

*通过下标操作符进行赋值时,并不会添加任何元素

*ivec[index] = index + 1; ERROR

*/

ivec.push_back(index + 1);

}

for (vector<int>::size_type index = 0; index != ivec.size(); ++index)

{

cout << ivec[index] << endl;

}

for (vector<int>::size_type index = 0; index != ivec.size(); ++index)

{

//对于已经存在的元素

ivec[index] = 333;

}

for (vector<int>::size_type index = 0; index != ivec.size(); ++index)

{

cout << ivec[index] << endl;

}

7、试图获取不存在的元素必然导致运行时错误,但是,不能确保执行过程中可以捕捉到这类错误!

程序运行时总会以某种有趣的方式失败@_@

[cpp] view
plaincopy





//P83 习题3.13 (1)

int main()

{

freopen("input.txt","r",stdin);

vector<int> ivec;

int value;

while (cin >> value)

{

ivec.push_back(value);

}



for (vector<int>::size_type index = 0; index < ivec.size() - 1; index += 2)

{

cout << ivec[index] + ivec[index + 1] << endl;

}

if (ivec.size() % 2)

{

cout << "The last element " << ivec[ivec.size() - 1]

<< " is not been summed!" << endl;

}

return 0;

}

[cpp] view
plaincopy





//(2)

int main()

{

freopen("input.txt","r",stdin);

vector<int> ivec;

int value;



while (cin >> value)

{

ivec.push_back(value);

}

vector<int>::size_type length = ivec.size();

cout << "Length is: " << length << endl;



for (vector<int>::size_type index = 0; index < ivec.size()/2; ++index)

{

cout << ivec[index] + ivec[ivec.size() - 1 - index] << endl;

}

if (ivec.size() % 2)

{

cout << "The center element " << ivec[ivec.size() / 2]

<< " is not been summed!" << endl;

}

return 0;

}

[cpp] view
plaincopy





//习题3.14

int main()

{

freopen("input.txt","r",stdin);

string word;

vector<string> strVec;



while (cin >> word)

{

strVec.push_back(word);

}



for (vector<string>::size_type i = 0;i != strVec.size(); ++i)

{

for (string::size_type j = 0;j != strVec[i].size(); ++j)

{

strVec[i][j] = toupper(strVec[i][j]);

}

}



for (vector<string>::size_type index = 0;index != strVec.size(); ++index)

{

cout << strVec[index] << ' ';

if (!((index+1) % 8))

{

cout << endl;

}

}

return 0;

}


标准库类型(四)--biteset

序言:

位是用来保存一组项或条件的yes/no信息[标识]的简洁方法。

[cpp] view
plaincopy





#include <bitset>

using std::bitset;

正文:

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

和vector对象不同的是:bitset类型对象的区别在于其长度而不是类型。在定义bitest时,要在尖括号中说明给出他的长度。

[cpp] view
plaincopy





bitset<32> bitvec;

[cpp] view
plaincopy





//四种初始化bitset的方法

bitset<n> b;

bitset<n> b(u);

bitset<n> b(string);

bitset<n> b(string,pos,m);

2、位集合的位置编号从0开始,但是,他的顺序是从右往左排的!以0位开始的位串是低阶位,以n结尾的是高阶位!



[cpp] view
plaincopy





//用unsigned值初始化bitset对象

bitset<128> b(0Xffff);

bitset<32> b(0Xffff);

bitset<16> b(0Xffff);

bitset<16> b(10);

[cpp] view
plaincopy





/*

*用string对象初始化bitset对象

*仍然是从右往左:string对象的最右边字符,用来初始化bitset对象的低阶位!

*/

string str("1100");

bitset<16> b(str);

string str("1000110011101111");

bitset<4> b(str); //从左边开始截取

[cpp] view
plaincopy





/*

*截取的是1101

*但是保存的时候是从右往左填充bitset对象

*/

string str("1000110111101111");

bitset<32> b(str,5,4);

[cpp] view
plaincopy





/*

*截取01111

*然后从右往左填充

*/

string str("1000110011101111");

bitset<32> b(str,str.size() - 5);

3、size_t类型

size_t类型是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小!

[cpp] view
plaincopy





size_t sz = bitvec.size();

4、bitset对象上的操作

[cpp] view
plaincopy





bool is_set = bitvec.any();

bool is_not_set = bitvec.none();

size_t sz = bitvec.count();

[cpp] view
plaincopy





for (size_t i = 0;i < bitvec.size(); ++i)

bitvec[i] = 1;

for (size_t i = 0;i != bitvec.size(); ++i)

{

bitvec.set(i);

}

bitvec.set();

bitvec.reset();

[cpp] view
plaincopy





for (size_t i = 0;i != bitvec.size(); ++i)

{

if (bitvec[i])

cout << 1 ;

else

cout << 0 ;

}

for (size_t i = 0;i != bitvec.size(); ++i)

{

if (bitvec.test(i))

cout << 1 ;

else

cout << 0 ;

}

5、获取bitset对象的值

[cpp] view
plaincopy





unsigned long value = bitvec.to_ulong();

[cpp] view
plaincopy





//观察程序

int main()

{

string str("00001000110011101111");

bitset<20> bitvec(str);



for (size_t i = 0;i != bitvec.size(); ++i)

{

if (bitvec.test(i))

cout << 1 ;

else

cout << 0 ;

}

cout << endl;

cout << bitvec << endl;

}

[cpp] view
plaincopy





<pre code_snippet_id="276569" snippet_file_name="blog_20140405_12_2743648"></pre><pre code_snippet_id="276569" snippet_file_name="blog_20140405_13_1191269" name="code" class="cpp"><pre code_snippet_id="276569" snippet_file_name="blog_20140405_13_1191269"></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>



<div style="padding-top:20px">

<p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

</div>

</pre>

本文借鉴:http://blog.csdn.net/column/details/zjf666.html?&page=6



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: