2

I have create this program to do Distance Proximity Analysis from foodbanks.dat and residences.dat it work fine in console but I want help to be able to run in on MPI. Just how can vector to C array because in MPI will not accept C++ vector also how to make each block of operation(read file and populate the vectors,calculate the distance to get the closet foodback andAnalysis the range in KM ) into separate functions

2
  • 2
    If using a C++11 std::vector vec; you could use vec.data() to access to some array pointer, and feed that to MPI routines. Commented Nov 10, 2013 at 20:08
  • @BasileStarynkevitch even without C++11 you can use feed &vec[0] to the mpi function routines but i'll be using vec.data() going foward Commented Nov 10, 2013 at 20:12

1 Answer 1

2

std::vector uses an array internally, so all you have to do is get a hold of that array.

If you have C++11 you can use std::vector::data to get a pointer to the array inside the of the std::vector.

If you don't have C++11 you can always use std::vector::front like so &front() to get the pointer to the array.

Just make sure that when you are doing this that you don't increase the size of the array inside the vector after getting the pointer because that will invalidate your pointer.

Code example:

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> myVec;
    myVec.push_back(0);
    myVec.push_back(1);

    //int* myArrayPointer = myVec.data(); // For C++11
    int* myArrayPointer = &myVec.front(); // For pre-C++11

    for(std::size_t i = 0; i < myVec.size(); ++i)
    {
        std::cout << myArrayPointer[i];
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ninjaDeveloper Okay, I gave you a second option for when you don't have C++11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.