1 Star 0 Fork 0

gitstr/modern-cpp-tutorial

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
4.1.linear.container.cpp 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
Changkun Ou 提交于 2019-07-20 01:17 +08:00 . book: translation of ch04
//
// 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 0
std::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 3
std::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 5
std::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 0
std::cout << "capacity:" << v.capacity() << std::endl; // output 8
// Additional memory can be returned to the system via the shrink_to_fit() call
v.shrink_to_fit();
std::cout << "size:" << v.size() << std::endl; // output 0
std::cout << "capacity:" << v.capacity() << std::endl; // output 0
std::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 implicitly
foo(&arr[0], arr.size());
foo(arr.data(), arr.size());
// more usage
std::sort(arr.begin(), arr.end());
for(auto &i : arr)
std::cout << i << std::endl;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitstr/modern-cpp-tutorial.git
git@gitee.com:gitstr/modern-cpp-tutorial.git
gitstr
modern-cpp-tutorial
modern-cpp-tutorial
master

搜索帮助