CS106L课程:第二讲 Containers

Sequence Containers

  • uniform initialization (curly braces)
  • code snippet
const int kNumInts = 200;
std::vector<int> vec(kNumInts);
std::generate(vec.begin(), vec.end(), rand);
std::sort(vec.begin(), vec.end());
std::copy(vec.begin(), vec.end(), 
		std::ostream_iterator<int>(cout, "\n"));

" vector is the type of sequence that should be used by default… deque is the data structure of choice when most insertions and deletions take place at the beginning or at the end of the sequence."
一C++ ISO Standard (section 23.1.1.2)

教材链接:http://web.stanford.edu/class/cs106l/full_course_reader.pdf

Associate Containers && Iterator

std::map<T1, T2> v.s std::unordered_map<T1, T2>
std::set<T> v.s std::unordered_set<T>
// Map/Set:sorted, faster to iterate through a range of elements.
// Unordered map/set: faster to access individual elements by key.
  • Iterators allow iteration over any container, whether it is ordered or not.
auto it = std::find(vec.begin(), vec.end(), elemToFind);
if(it != vec.end()) cout << "Found" << endl;
else cout << "Not Found" << endl;

map.count(key);
std::find(map.begin(), map.end(), key); // find() is faster.
  • list: doubly linked list.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值