Ho due array values
e keys
entrambi della stessa lunghezza. Voglio ordinare la matrice values
usando l'array keys
come chiavi. Mi è stato detto che l'iteratore zip di boost è lo strumento giusto per bloccare due array insieme e fare loro roba allo stesso tempo.boost zip_iterator e std :: sort
Ecco il mio tentativo di utilizzare boost :: zip_iterator per risolvere il problema di ordinamento che non riesce a compilare con gcc
. Qualcuno può aiutarmi a risolvere questo codice?
il problema sta nella linea
std::sort (boost::make_zip_iterator(keys, values ), boost::make_zip_iterator(keys+N , values+N));
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
int main(int argc, char *argv[])
{
int N=10;
int keys[N];
double values[N];
int M=100;
//Create the vectors.
for (int i = 0; i < N; ++i)
{
keys[i] = rand()%M;
values[i] = 1.0*rand()/RAND_MAX;
}
//Now we use the boost zip iterator to zip the two vectors and sort them "simulatneously"
//I want to sort-by-key the keys and values arrays
std::sort (boost::make_zip_iterator(keys, values ),
boost::make_zip_iterator(keys+N , values+N )
);
//The values array and the corresponding keys in ascending order.
for (int i = 0; i < N; ++i)
{
std::cout << keys[i] << "\t" << values[i] << std::endl;
}
return 0;
}
NOTA: Messaggio di errore sulla compilazione
g++ -g -Wall boost_test.cpp
boost_test.cpp: In function ‘int main(int, char**)’:
boost_test.cpp:37:56: error: no matching function for call to ‘make_zip_iterator(int [(((unsigned int)(((int)N) + -0x00000000000000001)) + 1)], double [(((unsigned int)(((int)N) + -0x00000000000000001)) + 1)])’
boost_test.cpp:38:64: error: no matching function for call to ‘make_zip_iterator(int*, double*)’
Come sottolineato da Carl-cuoco, non v'è più recente (duplicato) [domanda] (http://stackoverflow.com/questions/13840998/sorting-zipped-locked-containers-in-c-using -boost-or-the-stl) in cui viene fornita una soluzione di lavoro. Si noti inoltre che la libreria della gamma di Eric Niebler fornisce view :: zip che [funziona proprio] (http://stackoverflow.com/a/32720638/554283). Detta biblioteca è stata proposta per la standardizzazione. –