[c++] vector struktur: wyszukanie tych samych łańcuchów pól struktur
Ostatnio zmodyfikowano 2014-02-11 11:20
alixir |
» 2014-02-11 10:44:07 Kefirek jeśli szukasz rozwiązania w 'twoim' stylu to myślę, że to ci się spodoba #include <iostream> #include <string.h> #include <string> #include <vector>
struct sPerson { std::string name; int weight, height; };
int main() { sPerson people[] = { { "Jan", 1, 1 }, { "Dan", 2, 4 }, { "Jan", 4, 16 }, { "Sam", 3, 9 }, { "Dan", 3, 9 }, { "Cezar", 2, 4 } }; std::vector < sPerson > vPeople; for( int i = 0; i < 6; i++ ) vPeople.push_back( people[ i ] ); std::vector < sPerson > vSum; vSum.push_back( vPeople[ 0 ] ); bool exists; for( unsigned i = 1; i < vPeople.size(); i++ ) { exists = false; for( unsigned j = 0; j < vSum.size(); j++ ) { if( strcmp(( vPeople[ i ].name ).c_str(),( vSum[ j ].name ).c_str() ) == 0 ) { vSum[ j ].weight += vPeople[ i ].weight; vSum[ j ].height += vPeople[ i ].height; exists = true; } } if( !exists ) vSum.push_back( vPeople[ i ] ); } for( unsigned i = 0; i < vSum.size(); i++ ) std::cout << vSum[ i ].name << " " << vSum[ i ].weight << " " << vSum[ i ].height << std::endl; return 0; }
|
|
Kefirek Temat założony przez niniejszego użytkownika |
» 2014-02-11 11:20:51 Wielkie dzięki - właśnie takiego rozwiązania szukałem(prosty kod, mało pisania i przede wszystkim skuteczny). Pozdrawiam Kefirek
|
|
1 2 « 3 » |