c + + learning notes - IO, sequential containers, and associative containers

IO

File mode

  • in: read when opening a file;
  • out: write when opening a file (empty the file stream existing in the cloud by default);
  • app: find the end of the file before each write (the current file data will not be cleared);
  • ate: locate the file at the end of the file immediately after opening the file;
  • trunc: empty the existing file stream when opening a file;
  • Binary: IO operation in binary mode;

Sequential container

type

  • vector: variable array
  • deque: double ended queue, supporting fast random access
  • List: bidirectional linked list
  • forward_list: one way linked list
  • Array: fixed array
  • string

initialization

  • C c;
  • C c1(c2); c1 and C2 must be of the same type (same container, same generics)
  • C c1 = c2
  • C c{a, b, c}
  • C c = {a, b, c}
  • C c(b, e); b. E stands for iterator F
  • C seq(n); Initialize container size
  • C seq(n, t); Initialization size, and each is t

assign

Copy after assignment is supported

list<string> a;
vector<const char*> b;
a = b; // illegal
a.assign(b.begin(), b.end()); // legitimate

Container relationship comparison rules

Compare one by one in order and return according to the first unequal result

Application and return space

The container size will not be changed

  1. reserve
  2. shrink_to_fit (not guaranteed)

capacity

How many elements can be saved without allocating new memory space

Sequential Container Adaptors

  • stack (except array and forward_list)
  • queue (list and deque only)
  • priority_queue (vector and deque only)

generic algorithm

Read only algorithm

  • accumulate
  • equal

Write element algorithm

  • fill
  • back_inserter
  • copy
  • replace
  • replace_copy

Rearrangement algorithm

  • sort
  • unique

Associated container

  • map
  • set
  • Multi map: do not remap
  • Multi set: do not reset
  • unordered_map: hash map
  • unordered_set: hash set
  • unordered_mutilmap: hash does not duplicate map
  • unordered_ Mutil set: hash does not reset

Define build method

In addition to user-defined comparison functions, you can also set global comparison functions

bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
    return lhs.isbn() < rhs.isbn();
}

size_t hasher(const Sales_data &sd)
{
    return hash<string>()(sd.isbn());
}

multiset<Sales_data, decltype(compareIsbn)*> bookstore(compareIsbn);
// Bucket size, hash function pointer and same judgment function pointer
unordered_mutilset<Sales_data, decltype(hasher)*, decltype(compareIsbn)*> bookstore(42, hasher, compareIsbn);

Find elements in multi

Because multi allows the existence of repeating elements, it cannot be used at

// The first is to calculate the number and the starting position of the iterator and traverse it
string search_item("aquam");
auto entries = authors.count(search_item);
auto iter = authors.find(search_item);
while(entries) {
    ++iter;
    --entries;
}

// Second: lower/upper_bound, these two methods are not applicable to unordered containers
for(auto beg = authors.lower_bound(search_item), end = authors.upper_bound(search_item); beg != end; ++ beg)
    cout << beg-<second << endl;

// Third: equal_range, applicable to all associated containers, recommended
 for(auto pos = authors.equal_range(search_item); pos.first != pos.end; ++ pos.first)
    cout << pos.first->second << endl;

Unordered container management operation

unordered_set / unordered_map

  • c.bucket_count() - number of buckets in use
  • c.max_bucket_count() - the maximum number of buckets that can be held
  • c.bucket.size() - how many elements are there in the nth bucket
  • c. Bucket (k) - in which bucket is k
  • local_iterator - the iterator type that can be used to access the elements in the bucket
  • const_local_iterator
  • c.begin(n), c.end(n) - the beginning and end iterators of bucket n
  • c.cbegin(n), c.cend(n)
  • c.load_factor() - average number of elements per bucket, return float
  • c.max_ load_ Factor () - the average bucket size that C attempts to maintain
  • c.rehash(n) - reorganize storage and build hash
  • c.reserve(n) - reorganize the storage without rebuilding the hash

Keywords: C++

Added by Romeo20 on Mon, 17 Jan 2022 22:50:32 +0200