Search code examples
c++vectoriteratordeque

How to iterate a deque>>vector>>bytes to a function


I need to be able to call a function that is looking for an iterator of a complex data structure (pseudo-code) vector::deque::vector(uint8_t)::iterator. I need to be able to call it with a deque::vector(uint8_t); I can not figure out how to "iterate" it.

In the code segment below, I'm trying to call the MyFunkyFunc function with the someMoreBytes deque structure.

#include <cstdlib>
#include <vector>
#include <deque>
#include "stdint.h"

using namespace std;

void MyFunkyFunc(std::vector<std::deque<std::vector<uint8_t>>>::iterator itsIt)
{

}

int
main(int argc, char** argv)
{
    std::vector<std::deque<std::vector < uint8_t>>> bunchaBytes;
    std::deque<std::vector<uint8_t>> someMoreBytes;

    //... Put at least one element in bunchaBytes

    MyFunkyFunc(bunchaBytes.begin());
    MyFunkyFunc(someMoreBytes); // Problem is here

    return 0;
}

This code stub is a close as I can get to the original; I am unable to make any modifications to the MyFunkyFunc function, as it is in a library I have to link with. Many thanks in advance


Solution

  • If we assume that MyFunkyFunc was implemented properly as a template accepting an iterator parameter:

    template <typename I>
    void MyFunkyFunc (I itsIt) {
        //...
    }
    

    Then, you can just pass the address of someMoreBytes, since an iterator to a vector behaves the same as the address of an element of a vector.

    MyFunkyFunc(&someMoreBytes);
    

    Otherwise, you will need to redefine someMoreBytes to be a single element vector, and pass in the begin(), just as you did with bunchaBytes.