By sangmin park


2019-10-09 14:26:28 8 Comments

I'm trying to convert an array of Wall to a vector of Wall. The array is passed in as an argument and within the function I am trying to convert it using the insert(). I receive an error saying that "no instance of overloaded function "std::begin" matches the argument list -- argument types are: (Wall *)"

int func(int row, int col, Wall walls[]) {
  // Some code before ...

  // Converting array of Wall to vector
  std::vector<Wall> vecWalls;
  vecWalls.insert(vecWalls.begin(), std::begin(walls), std::end(walls));
 }

I have tried just creating another array of Wall within the function and using that to as an argument for the insert(). However, that did not create any issues.

Wall walls2[5];
std::vector<Wall> vecWalls;
vecWalls.insert(vecWalls.begin(), std::begin(walls2), std::end(walls2));

What is happening to my array of Wall when it is being passed as an argument.

3 comments

@Vlad from Moscow 2019-10-09 14:54:56

The standard functions std::begin and std::end may not be used with pointers as their arguments.

In the function declaration

int func(int row, int col, Wall walls[]) {

the parameter Wall walls[] is adjusted by the compiler to pointer type Wall *walls and the passed to the function array also is implicitly converted to pointer to its first element.

I suppose that within the function the array pointed to by walls is not changed.

In this case the function can be defined the following way

int func( int row, int col, const Wall walls[], size_t n ) {
  // Some code before ...

  // Converting array of Wall to vector
  std::vector<Wall> vecWalls( walls, walls + n );
 }

@Malov Vladimir 2019-10-09 14:47:47

Simple C array doesn't know anything about iterators, but you can fill std::vector using simple pointers on first and next after last array items.

int func(Wall* walls, size_t arraySize)
{
    std::vector<Wall> vecWalls;
    vecWalls.insert(vecWalls.begin(), &walls[0], &walls[arraySize]);

    return 0;
}

int main()
{
    Wall walls[10] = {};
    // Fill walls array

    const size_t arraySize = sizeof(walls) / sizeof(walls[0]);
    func(walls, arraySize);
}

Moreover, if you create new vector you can initialize it with items from you simple array like this:

std::vector<Wall> vecWalls2(&walls[0], &walls[arraySize]);

@churill 2019-10-09 14:46:41

The parameter Wall walls[] will decay to Wall *walls. Since you lose the length information you will need to pass the number of elements in that array along as parameter. Like this:

int func(int row, int col, Wall* walls, int len) {
    std::vector<Wall> vecWalls;
    vecWalls.insert(vecWalls.begin(), walls, walls + len);

    //or shorter:
    std::vector<Wall> vecWalls2(walls, walls + len);
}

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] Concatenating two std::vectors

23 Answered Questions

27 Answered Questions

[SOLVED] Easiest way to convert int to string in C++

1 Answered Questions

[SOLVED] Can't push_back into vector - gives no overload error

  • 2018-11-30 00:48:34
  • AnkilP
  • 139 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ vector

10 Answered Questions

[SOLVED] How to convert vector to array

  • 2010-05-27 17:14:40
  • ganuke
  • 307544 View
  • 315 Score
  • 10 Answer
  • Tags:   c++ arrays vector

2 Answered Questions

2 Answered Questions

[SOLVED] How to add a value to a vector of arrays?

  • 2017-01-24 04:36:26
  • braks
  • 77 View
  • 3 Score
  • 2 Answer
  • Tags:   c++ arrays vector

3 Answered Questions

[SOLVED] C++ how to push_back an array int[10] to std::vector<int[10]>?

  • 2016-07-06 01:17:44
  • linrongbin
  • 2609 View
  • 4 Score
  • 3 Answer
  • Tags:   c++ arrays vector stl

Sponsored Content