//// 4.1.linear.container.cpp// modern c++ tutorial//// created by changkun at changkun.de// https://github.com/changkun/modern-cpp-tutorial//#include <iostream>#include <array>#include <vector>void foo(int *p, int len) {for (int i = 0; i != len; ++i) {std::cout << p[i] << std::endl;}}int main() {std::vector<int> v;std::cout << "size:" << v.size() << std::endl; // output 0std::cout << "capacity:" << v.capacity() << std::endl; // output 0// As you can see, the storage of std::vector is automatically managed and// automatically expanded as needed.// But if there is not enough space, you need to redistribute more memory,// and reallocating memory is usually a performance-intensive operation.v.push_back(1);v.push_back(2);v.push_back(3);std::cout << "size:" << v.size() << std::endl; // output 3std::cout << "capacity:" << v.capacity() << std::endl; // output 4// The auto-expansion logic here is very similar to Golang's slice.v.push_back(4);v.push_back(5);std::cout << "size:" << v.size() << std::endl; // output 5std::cout << "capacity:" << v.capacity() << std::endl; // output 8// As can be seen below, although the container empties the element,// the memory of the emptied element is not returned.v.clear();std::cout << "size:" << v.size() << std::endl; // output 0std::cout << "capacity:" << v.capacity() << std::endl; // output 8// Additional memory can be returned to the system via the shrink_to_fit() callv.shrink_to_fit();std::cout << "size:" << v.size() << std::endl; // output 0std::cout << "capacity:" << v.capacity() << std::endl; // output 0std::array<int, 4> arr= {1,4,3,2};//int len = 4;//std::array<int, len> arr = {1,2,3,4}; // illegal, size of array must be constexpr// C style parameter passing// foo(arr, arr.size()); // illegal, cannot convert implicitlyfoo(&arr[0], arr.size());foo(arr.data(), arr.size());// more usagestd::sort(arr.begin(), arr.end());for(auto &i : arr)std::cout << i << std::endl;return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。