Sign in to follow this  
Kenny77

Vector Problem

Recommended Posts

I just created this program using vectors, as a little refresher, and I can't seem to get it to work
#include <iostream>
#include <conio.h>
#include <vector>

using namespace std;

int main()
{
    vector <int> num( 25, 50, 75 );
    
    cout << num.at(0) << endl;
    cout << num.at(1) << endl;
    cout << num.at(2) << endl;
    
    getch();
    return 0;
}


I get the error: no matching function for call to 'std::vector<int::allocator<int> > vector(int,int,int)'

Share this post


Link to post
Share on other sites
You can't specify the contents of the vector in the constructor. One fairly common way to work around this is to construct from an array:
#include <iostream>
#include <vector>

using namespace std;

// a handy pair of templates for getting pointers to the
// first and one-past-the-end elements of a stack array
template < typename Type, std::size_t n >
Type const * begin(Type (&array)[n])
{
return array;
}

template < typename Type, std::size_t n >
Type const * end(Type (&array)[n])
{
return array + n;
}

int main()
{
int num_data[] = {25, 50, 75};

// use iterator range to construct vector
vector <int> num(begin(num_data), end(num_data));

cout << num.at(0) << endl;
cout << num.at(1) << endl;
cout << num.at(2) << endl;
}

Σnigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this