zoukankan      html  css  js  c++  java
  • C++ Primer笔记1_转义字符_标准库类型string_标准库类型vector

    1.转义字符

    一般有两种方式:

    x后紧跟1个或多个十六进制数字、或后紧跟1、2、3个八进制数字,当中数字部分是字符相应的数值。

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	bool b = 10;
    	bool b1 = true;
    	bool b2 = false;
    
    	cout << b << endl;
    	cout << b1 << endl;
    	cout << b2 << endl;
    
    	cout << "115" << endl;// M
    	cout << "x5B" << endl;// [
    	cout << "x21" << endl;// !
    
    	return 0;
    }
    


    2.string对象
    使用时包括:#include <string>
    初始化string对象的方式:
    string s1;
    string s2(s1);
    string s2 = s1;
    string s3("value");//s3是字面值副本除空格
    string s3 = "value";
    string s4(n, 'c');//s4为n个c组成的串

    当把string对象和字符字面值及字符串字面值在一条语句使用时,必须确保每一个加法运算符(+)的两側运算对象至少有一个是string
    string s2 = s1 + ","; //正确
    string s2 = "hello" + ","; //错误

    3.vector对象
    使用时包括:#include <vector>
    标准库类型vector表示对象的集合,当中全部对象的类型都同样。C++既有类模板,也有函数模板。vector属于类模板。
    vector<int> ivec;
    vector<Person> person;
    vector<vector<string>> file;

    定义举例:
    vector<int> ivec;
    vector<int> ivec2(ivec);
    vector<int> ivec3 = ivec;
    vector<string> svec(ivec);//错误 类型不同

    vector<int> v1(10);//v1有10个元素,都是0
    vector<int> v2{10};//v2有1个元素,为10
    
    vector<int> v1(10,1);//v1有10个元素,都是1
    vector<int> v2{10,1};//v2有2个元素,为10与1

    vector的经常使用操作:
    v.push_back(t): 向v尾端加入一个值为t的元素
    v.empty(): v为空返回真,否则返回假
    v.size(): 返回v中元素个数

    注意过去定义vector<vector<string>> file;时须要在外层vector对象的右括号与其元素类型之间加一个空格,如vector<vector<string> > file;最新的C++11标准不须要。





  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3825192.html
Copyright © 2011-2022 走看看