How to create, initialize and print vector in C++

1 Answer

0 votes
#include <iostream>  
#include <vector>
 
using std::vector;
using std::cout;
using std::endl;
 
void print(const vector<int>& vec) {
    for (const auto& element : vec) {
        cout << element << " ";
    }
    cout << endl;
}
 
int main()
{
    int valuse[] = { 88, 7, 22, 3, 4, 1 };
 
    vector<int> vec(std::begin(valuse), std::end(valuse));
 
    print(vec);
}

 
/*
run:
 
88 7 22 3 4 1
 
*/

 



answered Jan 14, 2018 by avibootz
edited Jun 18, 2024 by avibootz

Related questions

2 answers 116 views
1 answer 82 views
1 answer 140 views
140 views asked Feb 15, 2018 by avibootz
1 answer 76 views
1 answer 93 views
...