C++ Features

C++ Features

C++ has a lot of features, some of which are even useful! In addition to having all sorts of language widgets, it also has a sizeable standard library known as the Standard Template Library or STL.

  • auto
  • vector
  • map and unordered_map
  • set and unordered_set
  • pair and tuple

Auto Specifier

Auto Specifier

  • Let the compiler figure out your types for you!
  • It's a C++ 11 feature
    • Compile with g++ -std=c++11
  • More details

Auto Specifier Example

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
  auto cstring = "asdf";
  auto str = string("asdf");

  auto thingers = vector<int>();
  return 0;
}

For-each Loop

For-each Loop

  • For iterating over the values in things.
  • Works for arrays
  • Works for certain classes
    • std::vector
    • std::map
    • Or any other type with begin() and end() member functions
  • More details

For-each Loop Example

#include<iostream>
using namespace std;

int main()
{
  int nums[] = {1,2,3,4,5,6};

  for (auto i : nums)
  {
    cout << i * i << endl;
  }

  return 0;
}

Modifying Things with for-each loops

  • If you want to modify the contents of something over which you are iterating, you need to toss in a &.

Modifying Things Example

#include<iostream>
using namespace std;

int main()
{
  int nums[] = {1,2,3,4,5,6};

  for (auto& i : nums)
  {
    i--;
  }

  for (auto i : nums)
  {
    cout << i * i << endl;
  }

  return 0;
}

Vector

Vector

Vector Example

#include<iostream>
#include<vector>

using namespace std;

int main()
{
  vector<int> v;
  for (int i = 0; i < 10; i++)
  {
    v.push_back(i);
  }

  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << *it << endl;
  }

  return 0;
}

Pair

Pair

  • Sort of like a struct that has exactly two items.
  • You can't add more items to it; you can't take any away.
  • You can only modify the contents of the two items.
  • The two items can be of different types!
  • The two items are called first and second.
  • More details

Pair Example

#include<iostream>
#include<utility>
using namespace std;

int main()
{
  pair<int,string> origin = pair<int,string>(0,"bleep");
  pair<int,int> coord = make_pair(3,5);

  cout << "origin: (" << origin.first << ","
       << origin.second << ")" << endl;

  cout << "coord: (" << coord.first << ","
       << coord.second << ")" << endl;

  return 0;
}

Tuple

Tuple

  • tuples are fancier pairs
  • They're still fixed in size, but you can decide the size
  • The items in the tuple can also be of differing types
  • Unlike pair, we can't just use first and second to get stuff. We have to use the get() function.
    • Use get<N>(tup) to get the Nth item from a tuple (here called tup).
  • More details

Tuple Example

#include<iostream>
#include<tuple>

using namespace std;

int main()
{
  tuple<int,int,string> coord_name(2,4,"A");
  cout << get<2>(coord_name) << ": ("
       << get<0>(coord_name) << ","
       << get<1>(coord_name) << ")\n";

  // We can unpack the tuple, too!
  int x, y;
  tie(x, y, ignore) = coord_name;
  cout << "(" << x << "," << y << ")\n";

  return 0;
}

Multiple return from functions, sort of!

  • Using a tuple, you can (kind of?) return more than one thing from a function!
  • Python, Go, and many other programming languages allow this... and now C++ does!... -ish.

"Multiple return" Example

#include<iostream>
#include<tuple>
using namespace std;

tuple<int,int> divide(int divisor, int dividend)
{
  return make_tuple(divisor / dividend, divisor % dividend);
}

int main()
{
  int quotient, remainder;
  tie(quotient, remainder) = divide(13,5);

  cout << "13 / 5 = " << quotient
       << " with remainder " << remainder << endl;

  return 0;
}

Map

Map

  • A map maps keys to values.
  • It's sort of like an array, but your indices don't have to be ints!
  • Also, the map only contains values for the keys you set. It starts out empty.
  • More details

Map Example

#include<iostream>
#include<map>
#include<string>

using namespace std;

int main()
{
  map<string, int> ages;
  ages["rick"] = 70;
  ages["morty"] = 14;

  for (auto it = ages.begin(); it != ages.end(); it++)
  {
    // Dereferencing the iterator gives us key/value pairs
    pair<const string, int> p = *it;
    cout << p.first << " is "
         << p.second << " years old." << endl;
  }

  for (auto it = ages.begin(); it != ages.end(); it++)
  {
    // Alternatively, we can dereference/access in one step.
    cout << it->first << " is "
         << it->second << " years old." << endl;
  }

  return 0;
}

Set

Set

  • A set is like a vector, but...
    • It won't let you insert duplicate values
    • It keeps its values sorted
    • Other less mind-blowing things
  • More details

Set Example

#include<iostream>
#include<set>
using namespace std;

int main()
{
  set<int> nums;
  for (int i = 1; i < 10; i++)
  {
    nums.insert(i);
    nums.insert(i-1);
  }

  cout << "nums contains " << nums.size() << " elements." << endl;

  // Returns an iterator pointing to the item in the set with value 3.
  // If no such item exists, find() returns the end iterator.
  if (nums.find(3) != nums.end())
  {
    cout << "nums contains 3" << endl;
  }

  if (nums.find(42) == nums.end())
  {
    cout << "nums does not contain 42" << endl;
  }

  return 0;
}