Skip to content

Instantly share code, notes, and snippets.

@vbuterin2
Created September 24, 2020 14:51
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 vbuterin2/53d6500759bccde6edd266b1425b6bed to your computer and use it in GitHub Desktop.
Save vbuterin2/53d6500759bccde6edd266b1425b6bed to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
int main(void) {
// Create and init a vector
// 1. vector<int> variable; variable.push_back()
std::vector<int> lucky_numbers;
lucky_numbers.push_back(1);
lucky_numbers.push_back(100);
// 2. Initialize integer vectors with an initial value
std::vector<int> vecOfInts(5);
// 3. Initialize vector to 5 string obejcts with value "Hi"
std::vector<std::string> vecOfStr(5, "Hi");
// 4. Initialize vector with an array
std::string arr[] = {"first", "second", "third", "fourth"};
// 5. Initialize vector with a string array
std::vector<std::string> vecOfStr(arr, arr + sizeof(arr)/sizeof(std::string));
// 6. Initialize vector with std::list
std::list<std::string> listOfStr;
listOfStr.push_back("first");
listOfStr.push_back("sec")
std::vector(std::string) vecOfStr(listOfStr.begin(), listOfStr.end());
for (std::string str : vecOfStr)
{
std::cout << str << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment