zoukankan      html  css  js  c++  java
  • C++ std::vector 总结笔记

    Initialization

    #include<iostream>
    #include<vector>
    
    using namespace std;
    int main()
    {
    	vector<int> ivec(10, -1);
    	vector<int> ivec2(ivec);
    	for (auto x : ivec2) {cout << x << endl;}
    
    	vector<int> jvec(10); //initialized as 0s;
        for (auto x: jvec)
        	cout << x << endl;
    
        vector<int> zvec{1,2,3};
        vector<int> avec={1,2,3}; //zvec and avec are equivalent
        for (int i=0; i<zvec.size(); i++)
        {
        	std::cout << (zvec[i] == avec[i]) << endl;
        }
    
    	return 0;
    }
    

    Methods

    1. v.empty(): return true if v is empty
    2. v.size: return the number of elements in v
    3. v.push_back(t): add an element to v.
    4. v1==v2: return true if number and values are equal
    int main()
    {
    	vector<int> avec{1,2,3};
    	vector<int> bvec{1,2,3};
    	cout << (avec==bvec) << endl;
    	cout << (&avec==&bvec) << endl;
    
    	return 0;
    }
    
    1
    0
    [Finished in 0.9s]
    

    Iterator

    int main()
    {
    	string s("hello");
    	if (s.begin()!=s.end())
    	{
    		auto it = s.begin();
    		*it = toupper(*it);
    	}
    	cout << s << endl;
    	for (auto it = s.begin(); it !=s.end() && !isspace(*it); ++it)
    	{
    		*it = toupper(*it);
    	}
    	cout << s << endl;
    	return 0;
    
    }
    
    Hello
    HELLO
    [Finished in 0.8s]
    

    vector<int>::iterator it
    vector<int>::const_iterator it2: it2 can read but not write

  • 相关阅读:
    2021.2.28
    《构建之法》11~16章读后感
    《构建之法》6~10章读后感
    《构建之法》1~5章读后感
    4.7 wait notify
    4.8 wait,notify 的正确姿势
    4.9 park&unpark
    4.10 重新理解线程的状态转换
    第七章 Redis-6.2.1脚本安装
    第三十九章 Centos 7 系统优化脚本
  • 原文地址:https://www.cnblogs.com/yaos/p/14014211.html
Copyright © 2011-2022 走看看