//professional C++ 4th edition p369
#include
#include
#include
#include
template
void printVector(const std::vector& v){
for(auto& element : v) { std::cout << element << " ";}
std::cout << std::endl;
}
std::vector createVectorOfSize(size_t size) //p371
{
std::vector vec(size);
int contents = 0;
for (auto& i : vec) {
i = contents++;
}
return vec;
}
int main()
{
std::string str1 = “Hello”;
std::string str2 = “World”;
// Create a vector of references to strings.
std::vector<std::reference_wrapperstd::string> vec1{ ref(str1) };
vec1.push_back(ref(str2)); // push_back() works as well.
// Modify the string referred to by the second reference in the vector.
vec1[1].get() += “!”;
// The end result is that str2 is actually modified.
std::cout << str1 << " " << str2 << std::endl;
std::vector<int> vectorOne {1, 2, 3, 5};
std::vector<int> vectorTwo;
vectorOne.insert(cbegin(vectorOne) + 3, 4);
for(int i = 6; i <= 10; i++){
vectorTwo.push_back(i);
}
printVector(vectorOne);
printVector(vectorTwo);
vectorOne.insert(cend(vectorOne), cbegin(vectorTwo), cend(vectorTwo));
printVector(vectorOne);
vectorOne.erase(cbegin(vectorOne) + 1, cbegin(vectorOne) + 5);
printVector(vectorOne);
vectorTwo.clear();
vectorTwo.insert(cbegin(vectorTwo), 10, 100);
vectorTwo.pop_back();
printVector(vectorTwo);
std::vector<int> myVector;
myVector = createVectorOfSize(123);
std::vector<std::string> vec;
std::string myElement(5, 'a');
vec.push_back(myElement);
vec.push_back(move(myElement));
vec.push_back(std::string(5, 'a'));
vec.emplace_back(5, 'a');
std::vector<int> vec2 {1, 2, 3};
std::cout << size(vec2) << std::endl;
std::cout << empty(vec2) << std::endl;
int* data1 = vec2.data();
int* data2 = data(vec2);
}
/*
wannian07@wann