Home > Blockchain >  Can't use vector::assign with 1 parameter on vs code mac
Can't use vector::assign with 1 parameter on vs code mac

Time:09-23

At first I thought there was something wrong with my code, but even this gets compiler errors and says that I need to provide 2 arguments:

#include<iostream>
#include<vector>


std::vector<int>v;

int main()
{
    int m;
    std::cin>>m;
    v.assign(m);

}

If I use v.assign(m,-1); instead of v.assign(m);, it compiles and runs just fine.

Here are the error messages:

    error: no matching member function for call to 'assign'
v.assign(m);
~~^~~~

~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c  /v1/vector:623:9: note: candidate function template not viable: requires 2 arguments, but 1 was provided
        assign(_InputIterator __first, _InputIterator __last);
        ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c  /v1/vector:633:9: note: candidate function template not viable: requires 2 arguments, but 1 was provided
        assign(_ForwardIterator __first, _ForwardIterator __last);
        ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c  /v1/vector:635:10: note: candidate function not viable: requires 2 arguments, but 1 was provided
    void assign(size_type __n, const_reference __u);

What should I do?

CodePudding user response:

See the documentation for std::vector.assign(). assign(n, e) will fill the vector with n copies of e. There is no 1 argument call to assign.

  • Related