Harvest:
Vector<vector<int> >res, cannot be used directly with Res[j].push_back (number), because Res[j] is empty, not initialized
You can define vector<int> Innumer first, Res.push_back (Innumber).
Just find a set of matching outputs in sum, and I want to output all the matching combinations in the array.
1#include"stdafx.h"2#include"Vector"3#include"Map"4#include"iostream"5#include"unordered_map" //Unordered_map's header file6 using namespacestd;7 8 classMyClass9 {Ten Public: Onevector<vector<int> > Twosum (vector<int> &nums,inttarget) A { -unordered_map<int,int> hash;//the hash table,<key,value> with the initial alias as hash is int type - intSize =nums.size (); thevector<vector<int> > Res;//first define a vector<vector<int> > store all eligible combinations -vector<int> inIt;//store each combination of eligible - intj =0; - for(inti =0; i < size; i++) + { - intNumtofind = target-Nums[i]; + if(Hash.find (numtofind)! =hash.end ()) A { atInit.push_back (Hash[numtofind]);//put the number of each group into Init first - Init.push_back (i); -Res.push_back (inIt);//put this group in res -Init.clear ();//clear values for each group - } -Hash[nums[i]] = i;//Place the values in the vector in the map in } - returnRes; to } + }; - the int_tmain (intARGC, _tchar*argv[]) * { $vector<int> nums = {1,2,3,4,4,9,8,Ten };Panax Notoginseng inttarget =5; -vector<vector<int> >Res; the MyClass solution; +res =solution.twosum (nums, target); A intSize =res.size (); the for(inti =0; i < size; i++) + { - intVsize =res[i].size (); $cout <<"["; $ for(intj =0; J < Vsize; J + +) - { -cout << Res[i][j] <<" "; the } -cout <<"]";Wuyi } thecout <<Endl; -System"Pause"); Wu return 0; -}
2016.07.13-vector<vector<int>> Apply 2--two sum extension