STL容器的初始化操作总结

STL容器分为三部分

  • 顺序容器
  • 关联容器
  • 容器适配器

顺序容器

vector(常见的顺序容器)

(1)default 默认构造函数
vector();
explicit vector (const allocator_type& alloc);//该函数为vector()的一个编译器解释
(2)fill
explicit vector (size_type n, const allocator_type& alloc = allocator_type());
vector (size_type n, const value_type& val,const allocator_type& alloc = allocator_type());
(3)range 迭代器方式初始化
template vector (InputIterator first, InputIterator last,const allocator_type& alloc = allocator_type());
(4) copy 赋值构造函数
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
(5)move 移动构造函数
vector (vector&& x); vector
(vector&& x, const allocator_type& alloc);
(6)initializer list 初始化列表
vector (initializer_list<value_type> il, const allocator_type& alloc = allocator_type());

(1)空容器构造函数(默认构造函数)

vector();

构造一个没有元素的空容器。

	std::vector<int> value;//为空,里面什么都没有

(2)填充构造函数

explicit vector (size_type n, const allocator_type& alloc = allocator_type());

构造一个包含n个元素的容器。 每个元素都是val的副本(如果提供,可以不提供)。

	std::vector<std::string> words4(5, "Mo");
	std::cout << "words4: " << words4 << '\n';

(3)范围构造器

template vector (InputIterator first, InputIterator last,const allocator_type& alloc = allocator_type());

构造一个包含与范围[first,last)一样多的元素的容器,每个元素都从该范围内的相应元素以相同顺序进行位置构造。

    std::vector<std::string> words2(words1.begin(), words1.end());
    std::cout << "words2: " << words2 << '\n';

(4)赋值构造函数(并用分配器复制)

vector (const vector& x);

以相同的顺序构造一个容器,其中包含x中每个元素的副本。

    std::vector<std::string> words3(words1);
    std::cout << "words3: " << words3 << '\n';

(5)移动构造函数(并使用分配器移动)

vector (vector&& x);

构造一个获取x元素的容器。如果alloc被指定并且与x的分配器不同,则元素被移动。 否则,将不会构造任何元素(其所有权将直接转让)。x处于未指定但有效的状态。

    vector<int> value{1,2,3,4};
    vector<int> moveValue(move(value));

(6)初始化列表构造器

vector (initializer_list<value_type> il, const allocator_type& alloc = allocator_type());

用相同的顺序构造一个容器,其中包含il中每个元素的副本。

    std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"};
    std::cout << "words1: " << words1 << '\n';

list、forward_list、deque(双向队列)
list、forward_list、deque(双向队列)与vector一样,构造函数同样为六个,这里我就不写了,只有将上面的vector换为list、forward_list、deque(双向队列)就行

array
C++中的数组类型,封装固定大小数组的容器
根据集合初始化(aggregate initialization)规则初始化数组(注意,默认初始化可能会导致非类T的不确定值)

 	std::array<int, 3> a2 = {1, 2, 3};  
    std::array<std::string, 2> a3 = { std::string("a"), "b" };

关联容器

主要分为搜索树(set,map)和哈希表(unordered_set,unordered_map)两种
set、map、unordered_set、unordered_map构造函数和上面顺序容器类似,但是不存在fill constructors

(1)空容器构造函数(默认构造函数)

构造一个没有元素的空容器。

  std::set<std::string> a;
  a.insert("cat");
  a.insert("dog");
  a.insert("horse");
  for(auto& str: a) std::cout << str << ' ';
  std::cout << '\n';
 
  std::map<std::string, int> map1;
  map1["something"] = 69;
  map1["anything"] = 199;
  map1["that thing"] = 50;
  std::cout << "map1 = "; print_map(map1);

(2)范围构造器

构造一个包含与范围[first,last)一样多的元素的容器,每个元素都从该范围内的相应元素以相同顺序进行位置构造。

  std::set<std::string> b(a.find("dog"), a.end());
  for(auto& str: b) std::cout << str << ' ';
  std::cout << '\n';
  std::map<std::string, int> iter(map1.find("anything"), map1.end());

(3)赋值构造函数(并用分配器复制)

以相同的顺序构造一个容器,其中包含x中每个元素的副本。

  std::set<std::string> c(a);
  c.insert("another horse");
  for(auto& str: c) std::cout << str << ' ';
  std::cout << '\n';
  std::map<std::string, int> copied(map1);

(4)移动构造函数(并使用分配器移动)

构造一个获取x元素的容器。如果alloc被指定并且与x的分配器不同,则元素被移动。 否则,将不会构造任何元素(其所有权将直接转让)。x处于未指定但有效的状态。

  std::set<std::string> d(std::move(a));
  for(auto& str: d) std::cout << str << ' ';
  std::cout << '\n';
  std::cout << "moved-from set is ";
  for(auto& str: a) std::cout << str << ' ';
  std::cout << '\n';
  std::map<std::string, int> moved(std::move(map1));

(5)初始化列表构造器

用相同的顺序构造一个容器,其中包含il中每个元素的副本。

  std::set<std::string> e {"one", "two", "three", "five", "eight"};
  for(auto& str: e) std::cout << str << ' ';
  std::cout << '\n';
  std::map<std::string, int> moved(std::move(map1));
发布了17 篇原创文章 · 获赞 3 · 访问量 416

猜你喜欢

转载自blog.csdn.net/qq_41172631/article/details/105361201