C++(3)标准库类型

来源:互联网 发布:asm算法源码 编辑:程序博客网 时间:2024/06/05 08:47

标准库类型(一)--命名空间usingstring类型

引:

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

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

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

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


正文:

一、命名空间的using声明

1 using std::cin;

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

格式:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. using namespace::name;  
  2. //一旦使用了using声明,我们就可以直接引用名字,而不需要再引用该名字的命名空间!  

   示例:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  9.     int v1, v2;  
  10.     cin >> v1 >> v2;  
  11.   
  12.     cout << "The sum of " << v1   
  13.          << " and " << v2  
  14.          << " is " << v1 + v2 << endl;  
  15.   
  16.     return 0;  
  17. }  

2、在一种情况下,必须总是使用完全限定的标准库名字:在头文件中。

    通常,头文件中应该只定义确实必要的东西。请养成这个好习惯!



二、标准库string类型

string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <string>  
  2. using std::string;  

1string对象的定义和初始化

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //四种定义及初始化方式  
  2.     string s1;  
  3.     string s2(s1);  
  4.     string s3("value");  
  5.     string s4(n,'c');  

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

1)都是以'\0'结尾

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. string s1("value");  
  2. for (string::size_type i = 0;s1[i] != '\0'; ++i)  
  3.     cout << s1[i] << ' ';  
  4. cout << endl;  


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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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;  

3getline读取整行文本

    1)、getline函数从输入流的下一行读取,并保存读取内容到string中,但是不包括换行符

    2)、getline函数将stream参数作为返回值

    3)、由于getline在返回时丢弃换行符,所以换行符将不会保存在string对象中!


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //P72 习题3.5  
  2. int main()  
  3. {  
  4.     string line;  
  5.     while (getline(cin,line))  
  6.     {  
  7.         cout << line << endl;  
  8.     }  
  9.   
  10.     return 0;  
  11. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //3.5(2)  
  2. int main()  
  3. {  
  4.     string word;  
  5.     while (cin >> word)  
  6.     {  
  7.         cout << word << endl;  
  8.     }  
  9.   
  10.     return 0;  
  11. }  


4、string对象的操作 size与empty

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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;  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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.     }  

5string::size_type类型

    string类类型和许多其他库类型都定义了一些配套类型,通过这些配套类型,库类型的使用就能与机器无关(machine-independent)string::size_type类型可以保证足够大到能够存储任意string对象的长度。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. string str = "Hello World";  
  2. string::size_type length = str.size();  

6string关系操作符

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. string big = "big",small = "small";  
  2. if (big == small)   //big <= small,big >=  small,big != small,...  
  3. {  
  4.     //...  
  5. }  
  6. else  
  7. {  
  8.     //...  
  9. }  

7string对象的赋值

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. string s1,s2 = “value”;  
  2. s1 = s2;  

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


8、两个string对象相加

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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;  

9string对象str下标的取值范围:0str.size()-1

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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;  

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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;  

11string对象中的字符处理函数

   isalnum,isalpha,iscntrl,isdigit,isgraph,islower,isprint,ispunct,isspace,isupper,isxdigit,tolower,toupper...


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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. }  

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



引子:

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

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <vector>  
  2. using std::vector;  

【模板】

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

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

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



正文:

1vector对象的定义和初始化

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //vector的四种初始化方式  
  2. vector<T> v1;  
  3. vector<T> v2(v1);  
  4. vector<T> v3(n,i);  
  5. vector<T> v4(n);  

示例:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. vector<int> ivec1;  
  2. vector<int> ivec2(ivec1);  
  3. /* 
  4. *ERROR 
  5. *vector<string> strVec(ivec2); 
  6. */  
  7.   
  8. vector<int> ivec3(10,-1);  
  9. vector<string> strVec(10,"HI");  

2vector对象的值初始化

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

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

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



3vector对象的动态增长

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

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //P80 习题3.11 下面语句正确or错误?  
  2.     vector<vector<int>> ivec;           //在C++11中正确,在C++98/03中错误  
  3.     vector< vector<int> > ivec;             //正确  
  4.     vector<string> svec(10,"NULL");                   //正确  

4vector对象的size

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. vector<int>::size_type length = st.size();    //正确  
  2. vector::size_type lenth;            //错误  

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int main()  
  2. {  
  3.     string word;  
  4.     unsigned count = 0;  
  5.     vector<string> strVec;  
  6.   
  7.     while (cin >> word)  
  8.     {  
  9.         ++ count;  
  10.         strVec.push_back(word);  
  11.     }  
  12.   
  13.     if (count == strVec.size())  
  14.     {  
  15.         cout << "Equal!" << endl;  
  16.     }  
  17.     //C++程序员应习惯于用 != 来限定循环的约束条件  
  18.     for (vector<string>::size_type index = 0; index != strVec.size(); ++index)  
  19.     {  
  20.         cout << strVec[index] << endl;  
  21.     }  
  22.   
  23.     return 0;  
  24. }  

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


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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. vector<int> ivec;  
  2. for (vector<int>::size_type index = 0; index != 10; ++index)  
  3. {  
  4.     /* 
  5.     *必须是已经存在的元素才能使用下标操作符进行索引 
  6.     *通过下标操作符进行赋值时,并不会添加任何元素 
  7.     *ivec[index] = index + 1; ERROR 
  8.     */  
  9.     ivec.push_back(index + 1);  
  10. }  
  11. for (vector<int>::size_type index = 0; index != ivec.size(); ++index)  
  12. {  
  13.     cout << ivec[index] << endl;  
  14. }  
  15. for (vector<int>::size_type index = 0; index != ivec.size(); ++index)  
  16. {  
  17.     //对于已经存在的元素  
  18.     ivec[index] = 333;  
  19. }  
  20. for (vector<int>::size_type index = 0; index != ivec.size(); ++index)  
  21. {  
  22.     cout << ivec[index] << endl;  
  23. }  

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

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //P83 习题3.13 (1)  
  2. int main()  
  3. {  
  4.     freopen("input.txt","r",stdin);  
  5.     vector<int> ivec;  
  6.     int value;  
  7.     while (cin >> value)  
  8.     {  
  9.         ivec.push_back(value);  
  10.     }  
  11.   
  12.     for (vector<int>::size_type index = 0; index < ivec.size() - 1; index += 2)  
  13.     {  
  14.         cout << ivec[index] + ivec[index + 1] << endl;  
  15.     }  
  16.     if (ivec.size() % 2)  
  17.     {  
  18.         cout << "The last element " << ivec[ivec.size() - 1]  
  19.              << " is not been summed!" << endl;  
  20.     }  
  21.     return 0;  
  22. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //(2)  
  2. int main()  
  3. {  
  4.     freopen("input.txt","r",stdin);  
  5.     vector<int> ivec;  
  6.     int value;  
  7.   
  8.     while (cin >> value)  
  9.     {  
  10.         ivec.push_back(value);  
  11.     }  
  12.     vector<int>::size_type length = ivec.size();  
  13.     cout << "Length is: " << length << endl;  
  14.   
  15.     for (vector<int>::size_type index = 0; index < ivec.size()/2; ++index)  
  16.     {  
  17.         cout << ivec[index] + ivec[ivec.size() - 1 - index] << endl;  
  18.     }  
  19.     if (ivec.size() % 2)  
  20.     {  
  21.         cout << "The center element " << ivec[ivec.size() / 2]  
  22.              << " is not been summed!" << endl;  
  23.     }  
  24.     return 0;  
  25. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //习题3.14  
  2. int main()  
  3. {  
  4.     freopen("input.txt","r",stdin);  
  5.     string word;  
  6.     vector<string> strVec;  
  7.   
  8.     while (cin >> word)  
  9.     {  
  10.         strVec.push_back(word);  
  11.     }  
  12.   
  13.     for (vector<string>::size_type i = 0;i != strVec.size(); ++i)  
  14.     {  
  15.         for (string::size_type j = 0;j != strVec[i].size(); ++j)  
  16.         {  
  17.             strVec[i][j] = toupper(strVec[i][j]);  
  18.         }  
  19.     }  
  20.   
  21.     for (vector<string>::size_type index = 0;index != strVec.size(); ++index)  
  22.     {  
  23.         cout << strVec[index] << ' ';  
  24.         if (!((index+1) % 8))  
  25.         {  
  26.             cout << endl;  
  27.         }  
  28.     }  
  29.     return 0;  
  30. }  

标准库类型(四)--biteset



序言:

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <bitset>  
  2. using std::bitset;  

正文:

1bitset对象的定义和初始化

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bitset<32> bitvec;  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //四种初始化bitset的方法  
  2.     bitset<n> b;  
  3.     bitset<n> b(u);  
  4.     bitset<n> b(string);  
  5.     bitset<n> b(string,pos,m);  

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


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //用unsigned值初始化bitset对象  
  2.     bitset<128> b(0Xffff);  
  3.     bitset<32> b(0Xffff);  
  4.     bitset<16> b(0Xffff);  
  5.     bitset<16> b(10);  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2. *用string对象初始化bitset对象 
  3. *仍然是从右往左:string对象的最右边字符,用来初始化bitset对象的低阶位! 
  4. */  
  5.     string str("1100");  
  6.     bitset<16> b(str);  
  7.     string str("1000110011101111");  
  8.     bitset<4> b(str); //从左边开始截取  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2. *截取的是1101 
  3. *但是保存的时候是从右往左填充bitset对象 
  4. */  
  5.     string str("1000110111101111");  
  6.     bitset<32> b(str,5,4);  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2. *截取01111 
  3. *然后从右往左填充 
  4. */  
  5.     string str("1000110011101111");  
  6.     bitset<32> b(str,str.size() - 5);  

3size_t类型

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. size_t sz = bitvec.size();  

4bitset对象上的操作

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. bool is_set = bitvec.any();  
  2. bool is_not_set = bitvec.none();  
  3. size_t sz = bitvec.count();  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. for (size_t i = 0;i < bitvec.size(); ++i)  
  2.     bitvec[i] = 1;  
  3. for (size_t i = 0;i != bitvec.size(); ++i)  
  4. {  
  5.     bitvec.set(i);  
  6. }  
  7. bitvec.set();  
  8. bitvec.reset();  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. for (size_t i = 0;i != bitvec.size(); ++i)  
  2. {  
  3.     if (bitvec[i])  
  4.         cout << 1 ;  
  5.     else  
  6.         cout << 0 ;  
  7. }  
  8. for (size_t i = 0;i != bitvec.size(); ++i)  
  9. {  
  10.     if (bitvec.test(i))  
  11.         cout << 1 ;  
  12.     else  
  13.         cout << 0 ;  
  14. }  


5、获取bitset对象的值

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. unsigned long value = bitvec.to_ulong();  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //观察程序  
  2. int main()  
  3. {  
  4.     string str("00001000110011101111");  
  5.     bitset<20> bitvec(str);  
  6.   
  7.     for (size_t i = 0;i != bitvec.size(); ++i)  
  8.     {  
  9.         if (bitvec.test(i))  
  10.             cout << 1 ;  
  11.         else  
  12.             cout << 0 ;  
  13.     }  
  14.     cout << endl;  
  15.     cout << bitvec << endl;  
  16. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <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>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8.       
  9.         <div style="padding-top:20px">           
  10.             <p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>  
  11.         </div>  
  12. </pre>  
本文借鉴:http://blog.csdn.net/column/details/zjf666.html?&page=6


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 摩托车驾驶证年审过期一个月怎么办 摩托车驾驶证记满12分怎么办 b2驾驶证扣2分该怎么办 b2扣6分以上怎么办 审驾照晚了3天怎么办 考驾驶证3年到期怎么办 学习驾驶证明过期了怎么办 a2扣了12分怎么办 驾照a2扣6分了怎么办 a2本扣9分怎么办 驾驶证分扣3分怎么办? 异地换驾驶证没有居住证怎么办 b2开c1车扣分怎么办 驾照五次没考过怎么办 大车行驶证丢了怎么办 车的产权证丢了怎么办 车子行驶证掉了怎么办 定期的存折丢了怎么办 存折密码输错6次怎么办 营业执照原件丢失怎么办怎么注销 违章扣了14分怎么办 c1驾驶本过期了怎么办 考驾照没带身份证怎么办 上海扣满12分怎么办 美宝旅行证丢失怎么办 汽车证件全丢了怎么办 车的行驶本丢了怎么办 车和行驶证丢了怎么办 考驾照人在外地怎么办 外地考驾照没有居住证怎么办 考驾驶证预约密码忘了怎么办 考驾照密码忘了怎么办 考驾照的密码忘了怎么办 手机银行登录密码忘了怎么办 宽带账号或密码错误怎么办 车险过户联系不上原车主怎么办 换车了etc忘拆了怎么办 c1d驾驶证d证到期了怎么办 摩托车驾驶证过五年怎么办 没居住证想上东莞牌怎么办 外地考驾照需要暂住证怎么办