1 #include <iostream> 2 #include <vector> 3 #include <map> 4 #include <string> 5 #include <utility> 6 7 8 int Update(std::pair<int, int>a) { 9 std::pair<int, int> temp; 10 11 std::vector<int> key_row_1(10); 12 std::vector<int> counts_row_1(10); 13 14 std::vector<int> key_row_2(10); 15 std::vector<int> counts_row_2(10); 16 17 std::map<int, int>res_row_1; 18 std::map<int, int>res_row_2; 19 std::map<int, std::map<int,int>>res_sum; 20 std::map<int, int>res; 21 22 23 int index1 = (a.first % 2); 24 if(counts_row_1[index1]==0) { 25 key_row_1.insert(key_row_1.begin() + index1, a.first); 26 counts_row_1.insert(counts_row_1.begin() + index1, a.second); 27 } else { 28 temp.first = key_row_1[index1]; 29 temp.second = counts_row_1[index1]; 30 key_row_1.insert(key_row_1.begin() + index1, a.first); 31 counts_row_1.insert(counts_row_1.begin() + index1, a.second); 32 } 33 34 35 36 if(temp.second != 0) { 37 int index2 = (temp.first % 2); 38 if(counts_row_2[index2]==0) { 39 key_row_2.insert(key_row_2.begin() + index2, temp.first); 40 counts_row_2.insert(key_row_2.begin() + index2, temp.second); 41 } else { 42 if(temp.second > counts_row_2[index2]) { 43 key_row_2.insert(key_row_2.begin() + index2, temp.first); 44 counts_row_2.insert(key_row_2.begin() + index2, temp.second); 45 } 46 } 47 } 48 49 50 std::cout << "stage1" << std::endl; 51 for(int i=0; i<2; i++) { 52 std::cout << "(" << key_row_1[i] << "," << counts_row_1[i] << ")" << "; "; 53 res_row_1.insert(std::pair<int,int>(key_row_1[i], counts_row_1[i])); 54 } 55 std::cout << std::endl; 56 std::cout << "stage2" << std::endl; 57 for(int i=0; i<2; i++) { 58 std::cout << "(" << key_row_2[i] << "," << counts_row_2[i] << ")" << "; "; 59 res_row_2.insert(std::pair<int,int>(key_row_1[i], counts_row_1[i])); 60 } 61 std::cout << std::endl; 62 63 64 std::cout << "the final result as follow: " << std::endl; 65 66 res_sum.insert({1,res_row_1}); 67 res_sum.insert({2,res_row_2}); 68 69 auto iter = res_sum.begin(); 70 for(; iter != res_sum.end(); ++iter) { 71 res.insert(iter->second.begin(), iter->second.end()); 72 } 73 74 for(const auto &i : res) { 75 std::cout << i.first << ":" << i.second << "; "; 76 } 77 std::cout << '\n'; 78 79 } 80 81 82 int main(int argc, char* agrv[]) { 83 std::vector<std::pair<int, int> >p; 84 p.push_back(std::make_pair(1,5)); 85 p.push_back(std::make_pair(2,6)); 86 p.push_back(std::make_pair(3,7)); 87 p.push_back(std::make_pair(4,8)); 88 p.push_back(std::make_pair(5,9)); 89 90 for(int i=0; i<5; i++) { 91 int Update(p[i]); 92 } 93 }
在主函数中调用Update函数时,出现如下错误:
有待后续解决!!!
标签:std,temp,int,C++,second,key,counts,row From: https://www.cnblogs.com/hjxiamen/p/16743765.html