I need to find the element with the highest occurrence in an array of strings. I am not sure what to do, since I don't have much experience with this. I don't know pointers / hashtables.
I've already done this for integers, but I can't make it work for strings.
My version:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a[]={1,2,3,4,4,4,5};
int n = sizeof(a)/sizeof(int );
int *b=new int [n];
fill_n(b,n,0); // Put n times 0 in b
int val=0; // Value that is the most frequent
for (int i=0;i<n;i++)
if( ++b[a[i]] >= b[val])
val = a[i];
cout<<val<<endl;
delete[] b;
return 0;
}
Any help for finding the most frequently occurring element in the array of strings is appreciated!
First, consider using C++ containers like vectors instead of plain arrays. (Search for "array to vector" or such to convert between them if you need.)
Then, if you can use C++11, you can do something like this (without C++11 it would become a bit more lengthy, but still doable):
std::string most_occurred(const std::vector<std::string> &vec) {
std::map<std::string,unsigned long> str_map;
for (const auto &str : vec)
++str_map[str];
typedef decltype(std::pair<std::string,unsigned long>()) pair_type;
auto comp = [](const pair_type &pair1, const pair_type &pair2) -> bool {
return pair1.second < pair2.second; };
return std::max_element(str_map.cbegin(), str_map.cend(), comp)->first;
}
Here is a version compatible with older C++
bool comp(const std::pair<std::string,unsigned long> &pair1,
const std::pair<std::string,unsigned long> &pair2) {
return pair1.second < pair2.second;
}
std::string most_occurred(const std::vector<std::string> &vec) {
std::map<std::string,unsigned long> str_map;
for (std::vector<std::string>::const_iterator it = vec.begin();
it != vec.end(); ++it)
++str_map[*it];
return std::max_element(str_map.begin(), str_map.end(), comp)->first;
}
You could consider using a vector of strings and to use a map to count occurrences:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> a;
map<string, int> m;
// fill a with strings
a.push_back("a");
a.push_back("b");
a.push_back("b");
a.push_back("c");
a.push_back("c");
a.push_back("c");
a.push_back("d");
a.push_back("e");
// count occurrences of every string
for (int i = 0; i < a.size(); i++)
{
map<string, int>::iterator it = m.find(a[i]);
if (it == m.end())
m.insert(pair<string, int>(a[i], 1));
else
m[a[i]] += 1;
}
// find the max
map<string, int>::iterator it = m.begin();
for (map<string, int>::iterator it2 = m.begin(); it2 != m.end(); ++it2)
{
if (it2 -> second > it -> second)
it = it2;
}
cout << it -> first << endl;
return 0;
}
This solution is probably not the best one in terms of elegance and efficiency, however it should give the idea.
Related
I'm attempting to create an algorithm in C++ which will give me all of the possible combinations of a set of list items (input in a map format). I want to avoid duplicates and make sure to cover all possible combinations. To simplify the example, here's what the input may look like:
map<string, vector<string> > sandwichMap;
sandwichMap["bread"].push_back("wheat");
sandwichMap["bread"].push_back("white");
sandwichMap["meat"].push_back("ham");
sandwichMap["meat"].push_back("turkey");
sandwichMap["meat"].push_back("roastbeef");
sandwichMap["veggie"].push_back("lettuce");
sandwichMap["sauce"].push_back("mustard");
I'd feed this map into the algorithm, and it should spit out a vector with all of the possible combinations (using one of each key type):
wheat+ham+lettuce+mustard
wheat+turkey+lettuce+mustard
wheat+roastbeef+lettuce+mustard
white+ham+lettuce+mustard
white+turkey+lettuce+mustard
white+roastbeef+lettuce+mustard
It needs to work for any map of string vectors. So far I've tried and gotten close, but I end up with duplicate combinations and missed combinations:
sandwichList getCombinations(sandwichMap sMap)
{
locList retList;
int totalCombos = 1;
for (sandwichMapIt i = sMap.begin(); i != sMap.end(); ++i)
{
totalCombos *= i->second.size();
}
retList.resize(totalCombos);
int locCount;
for (sandwichMapIt a = sMap.begin(); a != sMap.end(); ++a)
{
locCount = 0;
for (locListIt l = a->second.begin(); l != a->second.end(); ++l)
{
for (unsigned int i = 0; i < totalCombos / a->second.size(); ++i)
{
retList[i + a->second.size() * locCount] += *l;
}
locCount++;
}
}
return retList;
}
Any help would be greatly appreciated!
Updated code:
#include <vector>
#include <map>
#include <list>
#include <iostream>
typedef std::vector<std::string> strVec;
typedef std::list<std::string> strList;
typedef std::map<std::string, strVec> sandwichMap;
int main()
{
sandwichMap sMap;
sMap["bread"].push_back("wheat");
sMap["bread"].push_back("white");
sMap["meat"].push_back("ham");
sMap["meat"].push_back("turkey");
sMap["meat"].push_back("roastbeef");
sMap["veggie"].push_back("lettuce");
sMap["sauce"].push_back("mustard");
strList finalSandwichList;
for (sandwichMap::iterator i = sMap.begin(); i != sMap.end(); ++i)
{
strList tmpSandwich;
for (strVec::iterator j = i->second.begin(); j != i->second.end(); ++j)
{
if (finalSandwichList.empty())
{
tmpSandwich.push_back(*j);
}
else
{
for (strList::iterator k = finalSandwichList.begin(); k != finalSandwichList.end(); ++k)
tmpSandwich.push_back(*k + "+" + *j);
}
}
tmpSandwich.swap(finalSandwichList);
}
for (strList::iterator i = finalSandwichList.begin(); i != finalSandwichList.end(); ++i)
{
std::cout << *i << std::endl;
}
return 0;
}
//solution
std::list<std::string> result;
for(auto i=sandwichMap.begin(); i!=sandwichMap.end(); ++i) {
std::list<std::string> new_result;
for(auto j=i->second.begin(); j!=i->second.end(); ++j) {
if(result.empty())
new_result.push_back(*j);
else
for(auto k=result.begin(); k!=result.end(); ++k)
new_result.push_back(*k + "+" + *j);
}
new_result.swap(result);
}
This should work :
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
map<string, vector<string>> sMap;
vector<string> add;
int sett[200], countt;
void solve(map<string, vector<string>>::iterator itt, int ct, vector<string> addd){
vector<string> tmp = itt->second;
if(ct == countt){
for(int j=0;j<addd.size();j++){
cout<<addd[j]<<" ";
}
cout<<endl;
return;
}
itt++;
for(int i=0;i<tmp.size();i++){
//cout<<tmp[i]<<" ";
addd.push_back(tmp[i]);
solve(itt, ct+1, addd);
vector<string>::iterator tempIt = addd.end();
addd.erase(tempIt--);
}
}
int main(){
sMap["bre"].push_back("wh");
sMap["bre"].push_back("whi");
sMap["me"].push_back("ham");
sMap["me"].push_back("tur");
sMap["me"].push_back("rr");
sMap["veg"].push_back("let");
sMap["sau"].push_back("mus");
countt = sMap.size();
solve(sMap.begin(), 0, add);
return 0;
}
I have used backtracking to evaluate every possible combination.
Note : it is in c++11 you might need to change some part of the code for lower version of c++
link to output : http://ideone.com/Ou2411
The code is kinda long because of the helper methods, but it does the job:
#include <vector>
#include <string>
#include <map>
#include <iostream>
using namespace std;
template <class T>
vector<T> Head(const vector<T> &v) {
return vector<T>(v.begin(), v.begin() + 1);
}
template <class T>
vector<T> Tail(const vector<T> &v) {
auto first = v.begin() + 1;
auto last = v.end();
return vector<T>(first, last);
}
template <class T>
vector<T> Concat(const vector<T> &v1, const vector<T> &v2) {
vector<T> result = v1;
result.insert(result.end(), v2.begin(), v2.end());
return result;
}
vector<vector<string>> CombineVectorWithScalar(const vector<vector<string>> &v, const string &scalar) {
vector<vector<string>> result = v;
for (unsigned i = 0; i < v.size(); i++) {
result[i].push_back(scalar);
}
return result;
}
vector<vector<string>> CombineVectorWithVector(const vector<vector<string>> &v1, const vector<string> &v2) {
if (v2.empty()) {
return vector<vector<string>>();
}
else {
auto headCombination = CombineVectorWithScalar(v1, v2.front());
auto tailCombination = CombineVectorWithVector(v1, Tail(v2));
return Concat(headCombination, tailCombination);
}
}
vector<string> GetKeys(const map<string, vector<string>> &mp) {
vector<string> keys;
for (auto it = mp.begin(); it != mp.end(); ++it) {
keys.push_back(it->first);
}
return keys;
}
vector<vector<string>> CombineMapValues(const map<string, vector<string>> &mp) {
vector<string> keys = GetKeys(mp);
vector<vector<string>> result;
auto &firstVector = mp.begin()->second;
for (auto it = firstVector.begin(); it != firstVector.end(); ++it) {
vector<string> oneElementList;
oneElementList.push_back(*it);
result.push_back(oneElementList);
}
vector<string> restOfTheKeys = Tail(keys);
for (auto it = restOfTheKeys.begin(); it != restOfTheKeys.end(); ++it) {
auto ¤tVector = mp.find(*it)->second;
result = CombineVectorWithVector(result, currentVector);
}
return result;
}
void PrintCombinations(const vector<vector<string>> & allCombinations) {
for (auto it = allCombinations.begin(); it != allCombinations.end(); ++it) {
auto currentCombination = *it;
for (auto itInner = currentCombination.begin(); itInner != currentCombination.end(); ++itInner) {
cout << *itInner << " ";
}
cout << endl;
}
}
int main() {
map<string, vector<string> > sandwichMap;
sandwichMap["bread"].push_back("wheat");
sandwichMap["bread"].push_back("white");
sandwichMap["meat"].push_back("ham");
sandwichMap["meat"].push_back("turkey");
sandwichMap["meat"].push_back("roastbeef");
sandwichMap["veggie"].push_back("lettuce");
sandwichMap["sauce"].push_back("mustard");
auto allCombinations = CombineMapValues(sandwichMap);
PrintCombinations(allCombinations);
return 0;
}
void generate_all(std::map<std::string,std::vector<std::string>>::iterator start,
std::vector<std::string::iterator> accomulator,
std::map<std::string,std::vector<std::string>>& sMap){
for (auto it=start; it!=sMap.end(); ++it){
for (auto jt=it->second.begin(); jt!=it->second.end(); jt++){
generate_all(start+1,accomulator.pus_back[jt],sMap);
}
}
if (accomulator.size() == sMap.size()){
// print accomulator
}
}
Call with generate_all(sMap.begin(),aVector,sMap);
If the map is too big to go recursively, you can always generate an equivalent iterative code.
This solution is not recursive. Basically what it does is the following:
Compute how many combinations are actually possible
Know that for each key in the map, you're going to have to add nrCombinations/nrItemsInKey of them in total.
You can see it as a tree growing, branching more and more the more keys you have visited.
If you keep track of how many there are, how spaced they are and where they start you can automatically fill all combinations.
Code
#include <vector>
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, std::vector<std::string> > sandwichMap;
sandwichMap["bread"].push_back("wheat");
sandwichMap["bread"].push_back("white");
sandwichMap["meat"].push_back("ham");
sandwichMap["meat"].push_back("turkey");
sandwichMap["meat"].push_back("roastbeef");
sandwichMap["veggie"].push_back("lettuce");
sandwichMap["sauce"].push_back("mustard");
sandwichMap["sauce"].push_back("mayo");
// Compute just how many combinations there are
int combinationNr = 1;
for ( auto it : sandwichMap ) {
combinationNr *= it.second.size();
}
std::vector<std::vector<std::string>> solutions(combinationNr);
// We start with empty lists, thus we only have one cluster
int clusters = 1, clusterSize = combinationNr;
for ( auto category : sandwichMap ) {
int startIndex = 0;
int itemsNr = category.second.size();
int itemsPerCluster = clusterSize / itemsNr;
for ( auto item : category.second ) {
for ( int c = 0; c < clusters; ++c ) {
for ( int i = 0; i < itemsPerCluster; ++i ) {
// We sequentially fill each cluster with this item.
// Each fill starts offset by the quantity of items
// already added in the cluster.
solutions[startIndex+i+c*clusterSize].push_back(item);
}
}
startIndex += itemsPerCluster;
}
clusters *= itemsNr;
clusterSize = combinationNr / clusters;
}
for ( auto list : solutions ) {
for ( auto element : list ) {
std::cout << element << ", ";
}
std::cout << "\n";
}
return 0;
}
I have 2 char arrays like "const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};".
For putting them into a vector I found a nice quick solution:
void fillVecTest()
{
const int ArrSize = 3;
const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};
const char *arr2[ArrSize] = {"Blah2", "Wibble2", "Shrug2"};
std::vector<std::string> vec1(arr1, arr1+ArrSize);
std::vector<std::string> vec2(arr2, arr2+ArrSize);
std::vector<std::string>::iterator l_It1Vec1;
std::vector<std::string>::iterator l_It = vec1.end();
l_It = find(vec1.begin(), vec1.end(), std::string("Blah1"));
if(l_It != vec1.end())
{
size_t l_pos = l_It - vec1.begin();
printf("found %s, pos=%u val: %s\n", l_It->c_str(),l_pos, vec2[l_pos].c_str());
}
}
Now I thought it should be also possible to put both directly into a map as arr1 is the key and arr2 is the value. I tried some ways but I didn't succeed.
void fillMapTest()
{
const int ArrSize = 3;
const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};
const char *arr2[ArrSize] = {"Blah2", "Wibble2", "Shrug2"};
std::map<std::string,std::string> map1;//(pair(arr1,arr1), pair(arr1+ArrSize,arr2+ArrSize));
std::map<std::string,std::string>::iterator l_It1Map1;
//l_It1Map1 = find(map1.begin(), map1.end(), std::string("Blah1"));
if(l_It1Map1 != map1.end())
{
printf("found key: %s, val: %s\n",l_It1Map1->first.c_str(), l_It1Map1->second.c_str());
}
}
int _tmain(int /*argc*/, _TCHAR* /*argv[]*/)
{
fillVecTest();
fillMapTest();
return 0;
}
I think that just the commented lines in function "fillMapTest" would need to be solved.
Constuctor and find don't work like I want.
Please has any STL expert an idea?
The easiest way to write this:
std::map<std::string, std::string> m {
{ "key1", "value1" },
{ "key2", "value2" },
};
This requires your compiler to support initializer lists (a feature of C++11).
std::map<std::string, std::string> m;
for(int i = 0; i < ArrSize; i++) {
std::pair<std::string, std::string> p =
std::make_pair(std::string(arr1[i]), std::string(arr2[i]));
m.insert(p);
}
If you really want to use the map constructor you need an iterator of pairs and the only way(i know) is to use a std::vector<std::pair<std::string, std::string> >::iterator but this seems to be an unneeded extra step to get the same result.
#include <vector>
#include <map>
#include <string>
std::vector<std::pair<std::string, std::string> > values;
for(int i = 0; i < ArrSize; i++) {
std::pair<std::string, std::string> p =
std::make_pair(std::string(arr1[i]), std::string(arr2[i]));
values.push_back(p);
}
std::map<std::string, std::string> m(values.begin(), values.end());
for(int i = 0; i < ArrSize; i++) {
m.insert(pair<string, string>(arr1[i], arr2[i]));
}
I am trying to get an output of the number of all the identical strings in a vector as part of a much larger program. After a lot of research I have managed to put something together that works but it seems messy and I was wondering if there was a better way to do it.
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
void setMap(string i);
void addMap(string i);
map<string, int> myMap;
int main()
{
vector<string> myVector;
string myArray[6]={"foo","foo","bar","roo","foo","bar"};
for (int i=0; i<6; i++)
{
myVector.push_back(myArray[i]);
}
for_each (myVector.begin(), myVector.end(), setMap);
for_each (myVector.begin(), myVector.end(), addMap);
for (map<string, int, less< string >>::const_iterator iter = myMap.begin();
iter != myMap.end(); ++iter )
cout <<iter->first<<'\t'<<iter->second<<endl;
return 0;
}
void setMap(string i)
{
myMap[i]=0;
}
void addMap(string i)
{
myMap[i]++;
}
This code works fine and gives me the output I was after but I'm not that keen on having to add 2 extra functions to make it work or having to make the map global. Any hints would be gratefully received.
Well the simplest way to not have the extra functions and not have the map as global would be to not use for_each.
for_each (myVector.begin(), myVector.end(), setMap);
for_each (myVector.begin(), myVector.end(), addMap);
becomes
map<string, int> myMap;
for (vector<string>::iterator i = myVector.begin(); i != myVector.end(); ++i)
myMap[*i]=0;
for (vector<string>::iterator i = myVector.begin(); i != myVector.end(); ++i)
++myMap[*i];
Once you done that you could also remove the first loop
map<string, int> myMap;
for (vector<string>::iterator i = myVector.begin(); i != myVector.end(); ++i)
++myMap[*i];
since the map values will be initialised to zero anyway.
What made you think you had to use for_each anyway?
Your setMap function is unnecessary.
Consider what this function does, should the map's key not be present.
void addMap(string i)
{
myMap[i]++;
}
The expression myMap[i] will add a new key to your map.
Since the value type is int, this new value will be int(), which is guaranteed to be 0.
What about this? Encapsulate the counting mechanism in a separate function for reusability.
// Iterator pair based interface
template <class Iterator>
std::map<typename Iterator::value_type,int>
count(Iterator begin, Iterator end) {
std::map<typename Iterator::value_type,int> counts;
for (Iterator i = begin; i != end; ++i)
counts[*i]++;
return counts;
}
// Sequence interface
template <class Sequence>
inline std::map<typename Sequence::value_type,int>
count(Sequence seq) {
return count(seq.begin(), seq.end());
}
Then simply use it like this:
// C++11
for (const auto & c : count(myVector))
cout << c->first << '\t' << c->second << endl;
// C++03
std::map<string,int> counts = count(myVector);
for (std::map<string,int>::const_iterator c = counts.begin(), e = counts.end(); c != e; ++c)
cout << c->first << '\t' << c->second << endl;
Simple demo
Under C++11, you can do this:
#include <string>
#include <unordered_map>
#include <iostream>
int main() {
std::string myArray[6] = {"foo","foo","bar","roo","foo","bar"};
std::unordered_map<std::string, size_t> m;
for (const auto& s : myArray)
++m[s];
for (const auto& p : m)
std::cout << p.first << "\t" << p.second << std::endl;
}
This prints:
foo 3
bar 2
roo 1
This works because m[s] will automatically insert s into m if not already there.
Using std::unordered_map (a hashtable) is likely to be cheaper than std::map (a balanced tree).
You can do something very similar under C++03, except the "for each" loops shown above would be replaced by the regular "for" loops.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <map>
using namespace std;
int main (int argc, char * const argv[]) {
string myArray[]={"foo","foo","bar","roo","foo","bar"};
int arr_length = 6;
vector<string> myVector(myArray, myArray + arr_length);
//Print contents of vector:
copy(myVector.begin(),
myVector.end(),
ostream_iterator<string>(cout, " ")
);
cout << endl;
map<string, int> myMap;
vector<string>::iterator pos;
for (pos=myVector.begin(); pos<myVector.end(); ++pos)
{
myMap[*pos] += 1;
}
map<string, int>::iterator mapPos;
for (mapPos=myMap.begin(); mapPos != myMap.end(); ++mapPos) {
cout << "word: " << mapPos->first << "\t"
<< "count: " << mapPos->second << endl;
}
return 0;
}
--output:--
foo foo bar roo foo bar
word: bar count: 2
word: foo count: 3
word: roo count: 1
i have an STL map ;
i would like to get the first non NULL value in the map;
is there an efficient/quick way to do that?
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
bool IsNotNull(const pair<const int, int>& i)
{
return i.second != 0;
}
int main() {
map<int, int> m;
m[0] = 0;
m[1] = 1;
map<int, int>::const_iterator it = find_if(m.begin(), m.end(), IsNotNull);
cout << it->second << endl;
return 0;
}
Ideone demo
There's nothing quicker than just looping through and finding what you're looking for
for (map<X,Y>::const_iterator i = m.begin(); i != m.end(); ++i)
{
if (i->second != NULL)
{
// do something with first non-NULL value
break;
}
}
In the program below I've a typedef map. What I want to do is to implement a hash table. I'm trying to use unordered_map since I heard that is the efficient as it takes O(1) time. I use my typedef map everywhere in my main program (another program that I'm working on) so I don't want to change that. I want to implement hash table in one of the functions and I'm trying to figure out how to insert the contents of my map into the hash table and search for the key later. I've inserted a comment in two places where I'm having trouble. Please help.
#include <iostream>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
typedef vector<int> v_t;
typedef set<int> s_t;
typedef map<s_t, v_t> m_t;
typedef m_t::iterator m_it;
typedef std::unordered_map<s_t, v_t> Mymap;
int main(){
m_t sample;
for (int i = 0; i < 100; i = i+2) {
v_t v;
for(int k = 100 ; k<=105 ; ++k)
v.push_back(k);
s_t k;
k.insert(i);
sample.insert(sample.end(), make_pair(k, v));
}
//---------Debug--------------------
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
cout << "Key: ";
copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
cout << " => Value: ";
copy (it->second.begin(),it->second.end(),ostream_iterator<double>(cout," "));
cout << endl;
}
//---------------------------------
Mymap c1;
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
c1.insert(Mymap::value_type(it->first,it->second)); // how to do this ?
}
s_t s;
s.insert(72);
if(c1.find(s)!=c1.end()) // does this work ?
cout << "Success" << endl;
return 0;
}
I appreciate any help or comments.
After reading Jason's comments I understand why i cannot use a std::set as a key in unordered_map so I tried to use std::string as a key but the find function won't work. Could you please help me.
Mymap c1;
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
v_t v1;
std::string key;
key.insert(key.begin(),it->first.begin(),it->first.end());
copy(it->second.begin(), it->second.end(),std::back_inserter(v1));
c1.insert(Mymap::value_type(std::make_pair(key,v1)));
}
string s = "72";
if((c1.find(s) != c1.end()) == true)
cout << "Success" << endl;
return 0;
The basic element you're missing to make this work is to define a hashing function for your std::set that you're using as the key. The STL already defines equality and lexicographical ordering for a std::set, so you can use it as the key-value in a std::map as-is without any problems. It does not define a hash function though, so that is something you're going to have to-do by overloading std::hash. This is fairly straight-forward, and can be done by defining the following function:
namespace std
{
template<>
struct hash<std::set<int> > : public std::unary_function<std::set<int>, size_t>
{
size_t operator()(const std::set<int>& my_set) const
{
//insert hash algorithm that returns integral type
}
};
}
The above functor object would return an integral type of size_t, and would take a std::set as the argument. You'll have to define it inside of namespace std so that std::unordered_map will recognize it. An "easy" algorithm could be simply summing the elements since you have a set of type int. There are more complex algorithms out there that would reduce the number of collisions such a simple algorithm would create at the expense of hashing time. Once you have this defined though, you shouldn't have any problems inserting your std::set key-values into an unordered_map, as well as creating new key-values and finding them in the hash table.
You can see an example of your source-code working at: http://ideone.com/DZ5jm
EDIT: Jason's code placed here for reference:
#include <iostream>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
namespace std
{
template<>
struct hash<set<int> > : public unary_function<set<int>, size_t>
{
size_t operator()(const std::set<int>& my_set) const
{
set<int>::iterator iter = my_set.begin();
int total = 0;
for (; iter != my_set.end(); iter++)
{
total += *iter;
}
return total;
}
};
}
typedef vector<int> v_t;
typedef set<int> s_t;
typedef map<s_t, v_t> m_t;
typedef m_t::iterator m_it;
typedef std::unordered_map<s_t, v_t> Mymap;
int main(){
m_t sample;
for (int i = 0; i < 100; i = i+2) {
v_t v;
for(int k = 100 ; k<=105 ; ++k)
v.push_back(k);
s_t k;
k.insert(i);
sample.insert(sample.end(), make_pair(k, v));
}
//---------Debug--------------------
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
cout << "Key: ";
copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
cout << " => Value: ";
copy (it->second.begin(),it->second.end(),ostream_iterator<double>(cout," "));
cout << endl;
}
//---------------------------------
Mymap c1;
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
c1.insert(Mymap::value_type(it->first,it->second)); // how to do this ?
}
s_t s;
s.insert(72);
if(c1.find(s)!=c1.end()) // does this work ?
cout << "Success" << endl;
return 0;
}