i trying convert code c# c++ lack of dictionary tables/enumerables etc making me difficult result needed in c++. can type of container/methods use in c++ result needed?
thanks in advance.
find c1 , it's count group c1 c2 > 0 , c3 < 4 order c1
table(c1,c2,c3) ( number of rows expected not finite - - can't use array structure ) 5 1 2 4 2 3 --> edited line make list 4 4 3 4 0 1 --> ignore row c2=0 3 1 3 2 1 5 --> ignore row c3 > 4 .....
.....
expected output(number of rows meeting criteria each c1): 3 1 4 2 5 1
you need @ least:
- a
struct
hold each c1/c2/c3 tuple (orstd::tuple
if use c++11). - a
std::vector
(array-like container, dynamic size) hold tuples. - a
std::map
(sorted associative container) act dictionary compute output.
i believe enough started, if have specific problem when writing code don't hesitate ask new questions.
edit according comments:
you're not missing much, elvena's solution almost need except lacks vector container store objects. quite straightforward:
#include <iostream> #include <map> #include <vector> #include <tuple> int main() { std::vector<std::tuple<int, int, int>> values; while (you_have_more_data) { int c1, c2, c3; // somehow read c1, c2, c3 cin/file/whatever values.push_back(std::make_tuple(c1, c2, c3)); } std::map<int, int> dict; // iterate on vector (auto = values.begin(); != values.end(); ++i) { // *i (dereferencing iterator) yields std::tuple<int, int, int> // use std::get access individual values in tuple // 0 => c1; 1 => c2; 2 => c3 (same order in std::make_tuple) if (std::get<1>(*i) > 0 && std::get<2>(*i) < 4) dict[std::get<0>(*i)] += 1; // see std::map::operator[] } // iterate on map , print items (auto = dict.begin(); != dict.end(); ++i) // *i (dereferencing iterator) yields std::pair<int, int> // writing (*i).first cumbersome // let's write i->first instead (this same, different notation) std::cout << i->first << " " << i->second << std::endl; return 0; }
Comments
Post a Comment