Ho questo codice:
std::set<unsigned long>::iterator it;
for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) {
u_long f = it; // error here
}
Non c'è ->firstvalore. Come posso ottenere il valore?
Risposte:
Devi dereferenziare l'iteratore per recuperare il membro del tuo set.
std::set<unsigned long>::iterator it;
for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) {
u_long f = *it; // Note the "*" here
}
Se disponi di funzionalità C ++ 11, puoi utilizzare un ciclo for basato su intervalli :
for(auto f : SERVER_IPS) {
// use f here
}
const u_long& f = *it;.
Usa solo *prima it:
set<unsigned long>::iterator it;
for (it = myset.begin(); it != myset.end(); ++it) {
cout << *it;
}
Questo lo dereferenzia e ti consente di accedere all'elemento su cui si trova attualmente l'iteratore.
Come iterate std :: set?
int main(int argc,char *argv[])
{
std::set<int> mset;
mset.insert(1);
mset.insert(2);
mset.insert(3);
for ( auto it = mset.begin(); it != mset.end(); it++ )
std::cout << *it;
}
for(auto i : mset) std::cout << i;