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
1 Answer
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;
}
1 Comment
Caesar
@ninjaDeveloper Okay, I gave you a second option for when you don't have C++11.
std::vector vec;you could use vec.data() to access to some array pointer, and feed that to MPI routines.&vec[0]to the mpi function routines but i'll be usingvec.data()going foward