Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'                initialization/init4.cc - Initialization - ambiguities.Lecture 2 - slide 26 : 29
Program 5

// Illustrates ambiguities in certain initializations.

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

int main(){
  using std::vector;
  using std::string;

  // A trap:
  vector<int> v1{99};     // A vector with single int element, with value 99 - prefer list initialization
  vector<int> v2(99);     // A vector of 99 ints with default value 0 - forces use of a Vector constructor

  // As expected:
  vector<string> v3{"hello"};   // A vector with single string element "hello"
  vector<string> v4("hello");   // ERROR: There is no vector constructor with a single string parameter

  for(auto x: v1) std::cout << x << std::endl;  // 99
  for(auto x: v2) std::cout << x << std::endl;  // 0 0 ... 0   (99 zeros)
  for(auto x: v3) std::cout << x << std::endl;  // hello

}