emplace back vector c 2b 2b

Solutions on MaxInterview for emplace back vector c 2b 2b by the best coders in the world

showing results for - "emplace back vector c 2b 2b"
Lia
27 Jan 2016
1// vector::emplace_back
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7  std::vector<int> myvector = {10,20,30};
8
9  myvector.emplace_back (100);
10  myvector.emplace_back (200);
11
12  std::cout << "myvector contains:";
13  for (auto& x: myvector)
14    std::cout << ' ' << x;
15  std::cout << '\n';
16
17  return 0;
18}
Nicki
10 Jan 2019
1#include <vector>
2#include <string>
3#include <cassert>
4#include <iostream>
5 
6struct President
7{
8    std::string name;
9    std::string country;
10    int year;
11 
12    President(std::string p_name, std::string p_country, int p_year)
13        : name(std::move(p_name)), country(std::move(p_country)), year(p_year)
14    {
15        std::cout << "I am being constructed.\n";
16    }
17    President(President&& other)
18        : name(std::move(other.name)), country(std::move(other.country)), year(other.year)
19    {
20        std::cout << "I am being moved.\n";
21    }
22    President& operator=(const President& other) = default;
23};
24 
25int main()
26{
27    std::vector<President> elections;
28    std::cout << "emplace_back:\n";
29    auto& ref = elections.emplace_back("Nelson Mandela", "South Africa", 1994);
30    assert(ref.year != 1984 && "uses a reference to the created object (C++17)");
31 
32    std::vector<President> reElections;
33    std::cout << "\npush_back:\n";
34    reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));
35 
36    std::cout << "\nContents:\n";
37    for (President const& president: elections) {
38        std::cout << president.name << " was elected president of "
39                  << president.country << " in " << president.year << ".\n";
40    }
41    for (President const& president: reElections) {
42        std::cout << president.name << " was re-elected president of "
43                  << president.country << " in " << president.year << ".\n";
44    }
45}
queries leading to this page
c 2b 2b emplace back object exampleemplace back c 2b 2bemplace back vector cppc 2b 2b vcetor emplace backemplace and insert c 2b 2b requiring std 3a 3aallocatorc 2b 2b vector emblace back erroremplace backemplace back in cppvector emplace back array c 2b 2bvector emplace back arrayc 2b 2b emplace back undefined reference tostd c 2b 2b vector emplace backemplace vs push backemplace backvector emblace backvector emplace back functionemplace back in vector c 2b 2bvector emplace back vs push backemplace back vectorvector emplace backc 2b 2b emplace back struct vectordifference between push back and emplace backvector emplace backemplace back qvectorvector emplace back c 2b 2bstd 3a 3avector 3a 3aemplace back 28 29emplace back cppstd vector emplace back plan structemplace back 28 29 c 2b 2bvector emplace back vectorc 2b 2bx 2b 2b emplace backstd 3a 3avector emplace backemplace back vectoe c 2b 2bvector c 2b 2b emplace backvector emplace back 28cin 29 emplace back 28 29 c 2b 2bc 2b 2b emplace back examplec 2b 2b emplace backvector emplace back c 2b 2bemplace back 28 29 gfgwhat does emplace back back return in c 2b 2bemplace and emplace backemplace back 28 29emplace back examplestd vector emplace backc 2b 2b emplace backemblace backemplace back in qt vectorvector emplace backc 2b 2b vector emplace backemplace back vector c 2b 2bc 2b 2b vector emplace backemplace back example of a vector of classvector emplace back vector c 2b 2bemplace back c 2b 2b vectorc 2b 2b place backemplace back vector c 2b 2b