Question #47372

#include "std_lib_facilities_4.h"

int main()
{
vector<int> v = { 5, 7, 9, 4, 6, 8 };
for (int x:v)
cout << x << '\n';
}

when i compile this, i got these two error "Error 1 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'Vector<std::string>'
2 IntelliSense: no instance of constructor "Vector<T>::Vector [with T=int]" matches the argument list
argument types are: (int, int, int, int, int, int)

what is wrong?

Expert's answer

Answer on Question #47372 - Programming, C++

#include "std_lib_facilities_4.h"
int main()
{
vector<int> v = {5, 7, 9, 4, 6, 8};
for (int x:v)
cout << x << '\n';
}


when i compile this, i got these two error "Error 1 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'Vector<std::string>'

2 IntelliSense: no instance of constructor "Vector<t>::Vector [with T=int]" matches the argument list argument types are: (int, int, int, int, int, int)

what is wrong?

Solution.

// Connecting library
#include <vector> // To use the vector
#include <iostream> // To use the cout
using namespace std; // To use the vector and cout
int main()
{
//1
int arr[] = {5, 7, 9, 4, 6, 8};
vector<int> v(arr, arr + 6);
//2
// vector<int> v = {5, 7, 9, 4, 6, 8}; // if the compiler supports the new standard
for (int i=0; i<v.size(); i++)
{
cout << v[i] << '\n';
}
// system("pause");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS