vector<int> intVector(10, 100); // Creates vector of 10 ints with value 100
vector<string> stringVector(10, "hello");
vector<int> intVector3({ 1, 2, 3, 4, 5, 6 });
vector<int> intVector1 = { 1, 2, 3, 4, 5, 6 };
vector<int> intVector2{ 1, 2, 3, 4, 5, 6 };
class Element
{
public:
Element() {}
virtual ~Element() {}
};
vector<Element> elementVector;
auto elementVector = make_unique<vector<Element>>(10);
vector<int> intVector(10);
// Other code . . .
intVector.assign(5, 100);
intVector.assign({ 1, 2, 3, 4 });
vector<int> vectorOne(10);
vector<int> vectorTwo(5, 100);
vectorOne.swap(vectorTwo);
vector<int> vectorOne(10);
vector<int> vectorTwo(10);
if (vectorOne == vectorTwo) {
cout << "equal!" << endl;
}
else {
cout << "not equal!" << endl;
}
vectorOne[3] = 50;
if (vectorOne < vectorTwo) {
cout << "vectorOne is less than vectorTwo" << endl;
}
else {
cout << "vectorOne is not less than vectorTwo" << endl;
}
vector<double> doubleVector;
// Initialize max to smallest number
double max = -numeric_limits<double>::infinity();
for (size_t i = 1; true; i++) {
double temp;
cout << "Enter score " << i << " (-1 to stop): ";
cin >> temp;
if (temp == -1) {
break;
}
doubleVector.push_back(temp);
if (temp > max) {
max = temp;
}
}
max /= 100.0;
for (vector<double>::iterator iter = begin(doubleVector);
iter != end(doubleVector); ++iter) {
*iter /= max;
cout << *iter << " ";
}
vector<string> stringVector(10, "hello");
for (auto iter = cbegin(stringVector); iter != cend(stringVector); ++iter) {
cout << *iter << endl;
}
vector<int> vectorOne = { 1, 2, 3, 5 };
vector<int> vectorTwo;
// Oops, we forgot to add 4. Insert it in the correct place
vectorOne.insert(cbegin(vectorOne) + 3, 4);
// Add elements 6 through 10 to vectorTwo
for (int i = 6; i <= 10; i++) {
vectorTwo.push_back(i);
}
printVector(vectorOne);
printVector(vectorTwo);
// Add all the elements from vectorTwo to the end of vectorOne
vectorOne.insert(cend(vectorOne), cbegin(vectorTwo), cend(vectorTwo));
printVector(vectorOne);
// Now erase the numbers 2 through 5 in vectorOne
vectorOne.erase(cbegin(vectorOne) + 1, cbegin(vectorOne) + 5);
printVector(vectorOne);
// Clear vectorTwo entirely
vectorTwo.clear();
// And add 10 copies of the value 100
vectorTwo.insert(cbegin(vectorTwo), 10, 100);
// Decide we only want 9 elements
vectorTwo.pop_back();
printVector(vectorTwo);
//The output of the program is as follows:
//1 2 3 4 5
//6 7 8 9 10
//1 2 3 4 5 6 7 8 9 10
//1 6 7 8 9 10
//100 100 100 100 100 100 100 100 100
vector<int> createVectorOfSize(size_t size)
{
vector<int> vec(size);
int contents = 0;
for (auto& i : vec) {
i = contents++;
}
return vec;
}
...
vector<int> myVector;
myVector = createVectorOfSize(123);
class Element
{
public:
Element(int i, const string& str) : mI(i), mStr(str) {}
private:
int mI;
string mStr;
};
vector<Element> vec;
Element myElement(12, "Twelve");
vec.push_back(myElement);
vec.push_back(move(myElement));
vec.push_back(Element(12, "Twelve"));
vec.push_back({12, "Twelve"});
vec.emplace_back(12, "Twelve");
vec.push_back({12, "Twelve"});
// Or
vec.emplace_back(12, "Twelve");