Created
September 24, 2020 14:51
-
-
Save vbuterin2/53d6500759bccde6edd266b1425b6bed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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