简介
要点
例子1vector内存布局 #include <iostream>#include <vector> int main(){ std::vector<int> v {2,4,5}; v.push_back(6); v.pop_back(); v[1] = 3; std::cout << v[2] << std::endl; for (int x : v) std::cout << x << ' '; std::cout << std::endl; v.reserve(8); v.resize(5, 0); for (int x : v) std::cout << x << ' '; std::cout << std::endl; std::cout << v.capacity() << std::endl; std::cout << v.size() << std::endl;}
例子2#include <iostream>#include <vector>using namespace std;struct p2d { p2d(int x_, int y_): x{x_}, y{y_} {} int x, y;}; int main(){ vector<p2d> v { p2d{2,3} }; // insert copy v.push_back( p2d{6,4} ); // construct in place with // constructor ↓ ↓ arguments v.emplace_back(9,7); // iterator ↓ to first pos v.emplace(begin(v), 5,8); for (p2d x : v) std::cout << x.x << ' ' << x.y << std::endl;} 例子3
|
|
来自: 山峰云绕 > 《c加加c井号面向对象》