Skip to content

Instantly share code, notes, and snippets.

@madduci
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madduci/be56fdf53842b18c5b2d to your computer and use it in GitHub Desktop.
Save madduci/be56fdf53842b18c5b2d to your computer and use it in GitHub Desktop.
Playing with vector
#include <iostream>
#include <vector>
int main()
{
std::vector<int> int_array; //array of integers, empty
std::vector<double> double_array_1; //array of doubles, empty
std::vector<double> double_array_2(10); //array of doubles, containing 10 elements initialized with 0.0
std::vector<double> double_array_3(10, 2.5f);//array of doubles, containing 10 elements initialized with 2.5
std::vector<double> double_array_4 {1.0, 2.0, 3.0, 4.0, 5.0}; //array initialized with some values
for(auto x:double_array_1) //loop into an array, C++11 way
std::cout << "Array Double 1: " << x << std::endl;
std::cout << std::endl;
for(auto x:double_array_2)
std::cout << "Array Double 2: " << x << std::endl;
std::cout << std::endl;
for(auto x:double_array_3)
std::cout << "Array Double 3: " << x << std::endl;
std::cout << std::endl;
for(auto x:double_array_4)
std::cout << "Array Double 4: " << x << std::endl;
std::cout << std::endl;
//adding an element to int_array
int_array.push_back(10000);
std::cout << "int_array contains " << int_array.size() << " elements" << std::endl;
//deleting an element from double_array_4
double_array_4.pop_back();
std::cout << "double_array_4 contains " << double_array_4.size() << " elements" << std::endl;
std::cout << std::endl;
//Which element is gone?
for(auto i = 0; i < double_array_4.size(); i++)
std::cout << "Array Double 4 (updated): " << double_array_4.at(i) << std::endl;
std::cout << std::endl;
//copy of a vector
auto copy_vector = double_array_4;
for(auto x:copy_vector)
std::cout << "copy_vector: " << x << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment