• 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

  • 相关阅读:
    数据类型
    java基础
    Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math(数论)
    Codeforces Round #655 (Div. 2) A. Omkar and Completion(构造)
    LibreOJ
    QT入门-QMainWindow类
    WCF 请求与响应参数大小设置
    Python 代码性能优化技巧
    lists,tuples and sets of Python
    SQL Language
  • 原文地址:https://www.cnblogs.com/yaos/p/12094219.html
Copyright © 2020-2023  润新知