vector内存布局
#include
#include
int main()
{
std::vector 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;
} https://wandbox.org/nojs/gcc-head
https://wandbox.org/nojs/clang-head#include
#include
using namespace std;
struct p2d {
p2d(int x_, int y_): x{x_}, y{y_} {}
int x, y;
};
int main()
{
vector 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;
} #include
#include
int main()
{
std::vector v {0,1,2,3,5,6};
auto i = std::begin(v) + 3;
//v.insert(i,8);
//std::cout << *i; // ERROR
//v.erase(i);
//std::cout << *i; // ERROR
std::cout << *i << std::endl;
i = v.insert(i,8);
std::cout << *i << std::endl;
i = v.erase(i);
std::cout << *i << std::endl;
for (int x : v) std::cout << x << ' ';
std::cout << std::endl;
} | 留言与评论(共有 0 条评论) “” |