Modern C++ (C++11 & beyond)
Variable Initialization
int a = 1;
int a{1};
print using std::cout << a << std::endl;
String Initialization
string s = "abhijit";
string s{"abhijit"};
print using std::cout << s << std::endl;
Vector Initialization
vector <int > v;
v.push_back(10 );
v.push_back(20 );
// print
std::cout << v.at(0 ) << std::endl; // print 0th i.e. 1st element
std::cout << v.at(1 ) << std::endl; // print 1st i.e. 2nd element
std::cout << v.at(2 ) << std::endl; // Gives Error: 'std::out_of_range'
map<string, string> capitals = {
{" UK" , " london" }, {" India" , " Delhi" }
};
map<string, string> capitals {
{" UK" , " london" }, {" India" , " Delhi" }
};
for (auto && i : capitals) {
std::cout << i.first << " : " << i.second << std::endl;
}
struct Person
{
string name;
int age;
};
Person p = {" abhijit" , 27 };
std::cout << p.name << " , " << p.age << std::endl;
namespace A {
namespace B {
namespace C {
void bar (){};
}
}
}
namespace A ::B::C {
void bar (){};
}
- calling in main function