Contents

CS106L

This article contains the notes and reports for projects in which I’m interested.

Notes

The notes is the collection of tips that I don’t know before, rather than a summary.

Structure and Reference

Structured Binding:

std::tuple<type1, type2, ..., type> x;
auto p = std::make_pair("s", 5);
auto [a, b] = p;

Uniform initialization:

Use brackets to initalize anything succinctly.

Make sure you’re completely clear what constructor you’re invoking when using uniform initialization

Make a copy for a const reference:

std::vector<int> copy = c_ref;
// copy is not a const reference.

Explicitly specify const and & with auto

Stream

Buffering: When buffer is full, write entire buffer to output.

// empty the buffer mannually
stream << std::flush;
stream << std::endl;

Chaining: overload function and return reference.

STL

Don’t provide functions which might be mistaken to be efficient when it’s not.

Deque: Two-end extendable linear structure.

Container

Container Adaptors Wrap the sequence containers with specified access methods.

template<
    class T,
    class Container = std::deque<T>
>class stack;

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

Associative Containers

std::set, std::map std::map<K, V> is a std::pair<const K, V>

Iterators

  • move forward
  • retrieve element
  • check if two claws are in the same place

containers provide the bouns.

Use ++iter instead of iter++. (The latter will store the old value of the iterator.)

STL

Template

Concept lifting

template <typename InputIt, typename DataType>
int count_occurrences(InputIt begin, InputIt end, DataType val) {
  int count = 0;
  for (auto iter = begin; iter != end; ++iter) {
    if (*iter == val) count++;
  }
  return count;
}
vector<string> v; count_occurrences(v.begin(), v.end(), test);

Lambda

[x](arguments) // captures x from surrounding scope by value
[x&](arguments) // captures x from surrounding scope by reference
[x, y](arguments) // captures x, y by value
[&](arguments) // captures everything by reference
[&, x](arguments) // captures everything except x by reference
[=](arguments) // captures everything by copy

Use lambda to implement generic function:

template <typename InputIt, typename DataType, typename UniPred>
int count_occurrences(InputIt begin, InputIt end, UniPred pred) {
  int count = 0;
  for (auto iter = begin; iter != end; ++iter) {
    if (pred(*iter) == val) count++;
  }
  return count;
}

This is similar to the implementation std::count:

template <class InputIt, class UnaryPredicate >
typename iterator_traits<InputIt>::difference_type
count_if( InputIt first, InputIt last, UnaryPredicate p);

Class

Template Class:

// vector.h
class vector<T> {
    T at(int i);
};
#include “vector.cpp”

// vector.cpp
void vector<T>::at(int i) {
    // this works
}

// main.cpp
#include “vector.h”
vector<int> a;
a.at(5);

// compile
// g++ -c main.cpp
// g++ main.o -o output

Member Types

// internal type vector<T>::iterator
template <typename T>
typename vector<T>::iterator vector<T>::insert(iterator pos,
int value) {

}

Used to make sure your clients have a standardized way to access important types. ● Lives in your namespace: vector::iterator. ● After class specifier, you can use the alias directly (e.g. inside function arguments, inside function body). ● Before class specifier, use typename. typename vector::iterator iter = vec.begin();

Still a little bit confusing

Const Correctness

Const Member • A const member function is a function that cannot modify any private member variables. • A const member function can only call other const member functions

A const object can only call the const member functions. (Exceptions: constructor and destructor)

Implement both const and non-const return type for const vectors and non-const vectors.

const_iterators:

The type of iterators usually depends on the corresponding containers.

  • iterator points to non-const objects

  • const_iterator points to const objects

  • const iterator is a const object

  • const vectors return const_iterators

  • vectors return iterators

const_iterator object itself is not const. const iterator is a const object

Operators