Summary of c++1114 STL points

  • 2020-09-28 09:01:15
  • OfStack

In c++, STL and STL contain a lot of practical data structures, such as vector,list,map,set, etc., which are commonly used by us. c++11 also makes a supplement to STL, making STL more and more rich in content and more and more choices.

1. std::array

Take a look at the following code:


#include <array>
#include <iostream>
int main()
{
  std::array<int, 4> arrayDemo = { 1,2,3,4 };
  std::cout << "arrayDemo:" << std::endl;
  for (auto itor : arrayDemo)
  {
    std::cout << itor << std::endl;
  }
  int arrayDemoSize = sizeof(arrayDemo);
  std::cout << "arrayDemo size:" << arrayDemoSize << std::endl;
  return 0;
}

As you can see from the above code, actually std::array It's no different than an array, except it adds iterator functionality.

2. std::forward_list

Take a look at the following code:


#include <forward_list>
#include <iostream>
int main()
{
  std::forward_list<int> numbers = {1,2,3,4,5,4,4};
  std::cout << "numbers:" << std::endl;
  for (auto number : numbers)
  {
    std::cout << number << std::endl;
  }
  numbers.remove(4);
  std::cout << "numbers after remove:" << std::endl;
  for (auto number : numbers)
  {
    std::cout << number << std::endl;
  }
  return 0;
}

std::forward_list Added linear table for c++11, and list The difference is that it's a one-way list, whereas list It's a bidirectional linked list. As we all know when we study data structures, linked lists have advantages over sequentially stored linear tables in inserting and deleting data. Therefore, list and Linear tables are used in frequent insertion and deletion scenarios forward_list Than using array , vector and deque It's much more efficient.

3. std::unordered_map

Take a look at the following code:


#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
  std::unordered_map<std::string, std::string> mymap =
  {
    { "house","maison" },
    { "apple","pomme" },
    { "tree","arbre" },
    { "book","livre" },
    { "door","porte" },
    { "grapefruit","pamplemousse" }
  };
  unsigned n = mymap.bucket_count();
  std::cout << "mymap has " << n << " buckets.\n";
  for (unsigned i = 0; i<n; ++i) 
  {
    std::cout << "bucket #" << i << " contains: ";
    for (auto it = mymap.begin(i); it != mymap.end(i); ++it)
      std::cout << "[" << it->first << ":" << it->second << "] ";
    std::cout << "\n";
  }
  return 0;
}

std::unordered_map with std::map The usage is pretty much the same, but the internal implementation of STL is quite different, std::map The data structure used is a red-black tree and is ordered, while std::unordered_map The inside is the implementation of the hash table, unordered. Theoretically, the lookup efficiency of hashing map is O(1), but in terms of storage efficiency, hashing map needs to increase the memory overhead of hashing table.

4. std::unordered_set

Take a look at the following code:


#include <iostream>
#include <string>
#include <unordered_set>
#include <set>
int main()
{
  std::unordered_set<int> unorder_set;
  unorder_set.insert(7);
  unorder_set.insert(5);
  unorder_set.insert(3);
  unorder_set.insert(4);
  unorder_set.insert(6);
  std::cout << "unorder_set:" << std::endl;
  for (auto itor : unorder_set)
  {
    std::cout << itor << std::endl;
  }
​
  std::set<int> set;
  set.insert(7);
  set.insert(5);
  set.insert(3);
  set.insert(4);
  set.insert(6);
  std::cout << "set:" << std::endl;
  for (auto itor : set)
  {
    std::cout << itor << std::endl;
  }
}

std::unordered_set The data storage structure is also the hash table structure. In addition to that, std::unordered_set Unlike set, which does not automatically sort when inserting, other USES are similar.

So that's c++11 & 14-ES68en Highlights summary details, more on c++11 & 14-ES71en information please pay attention to other related articles on this site!


Related articles: