Duplicate elements in array using Hash Table in C++ [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Can someone give an example of finding array duplicates using a Hash table and a function.
I am looking for some examples in C++.
The codes I am getting are all in Java.

One solution is to put in a hash table the element of the array (as key) and their number of occurrence (as value).
Then you copy the key of the hash table where the associated value is more than 1.
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
int main(int argc, char* argv[]) {
std::vector<int> vec{1,1,2,3,1,4,5};
std::map<int, int> m;
// We copy the element of the vector into the hash table
std::for_each(vec.begin(), vec.end(), [&m](auto & elt){ m[elt] += 1; });
std::vector<int> res;
// We select the key where the value is > 1
std::for_each(m.begin(), m.end(), [&res](auto & elt) { if(elt.second > 1) res.push_back(elt.first); });
}

Related

Best way to copy members from vector<Class> to vector<Member_Type> [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 months ago.
Improve this question
I have a vector of complicated structs (here std::pair<int, int>). I now want to copy a member (say std::pair::first) into a new vector. Is there a better (more idiomatic) way than
std::vector<std::pair<int, int>> list = {{1,2},{2,4},{3,6}};
std::vector<int> x_values;
x_values.reserve(list.size());
for( auto const& elem : list )
{
x_values.emplace_back(elem.first);
}
std::transform is often used for exactly this:
#include <algorithm> // transform
#include <iterator> // back_inserter
// ...
std::vector<int> x_values;
x_values.reserve(list.size());
std::transform(list.cbegin(), list.cend(), std::back_inserter(x_values),
[](const auto& pair) { return pair.first; });
In c++20 using std::ranges::views::transform and range algorithms one might do
#include <ranges> // std::views::transform
#include <algorithm> // std::ranges::copy
std::vector<int> x_values;
x_values.reserve(list.size());
std::ranges::copy(std::views::transform(list, [](const auto& pair) {
return pair.first; }), std::back_inserter(x_values));
or may be a just view over the pair.first (if the indention is to have a read only range)
#include <ranges> // std::views::transform
const auto x_values_view = std::views::transform(list, [](const auto& pair) {
return pair.first; }
);
Live Demo

initilize unordered_map with a vector in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I simply would like to initizlize an unordered_map with a vector.
When I have tried the code below, the code does not compile.
What is the shortest syntax to initilize unordered_map with a vector? How would you implement following method without using loop.
unordered_map<int, int> convertToMap(const vector<int>& v)
{
unordered_map<int, int> umap(v.begin(), v.end());
return umap;
}
I have found some resources, and I have tried to initialize through a range, but it does not worked either!
initialize-unordered-map
How to initialize unordered_map with vector?
If your vector contains pairs of elements, so that one could be interpreted as key and one as value then you can use range initialization:
#include <vector>
#include <unordered_map>
#include <iostream>
#include <utility>
int main()
{
std::vector<std::pair<int, int>> v = { {1,10}, {2,20}, {3,30} };
std::unordered_map<int, int> map(v.begin(), v.end());
std::cout << map[2] << std::endl;
return 0;
}
The above prints: 20

Extract vector from std::map<int, std::vector> [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my code
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef std::vector<int> VectorInt;
typedef std::map<int, VectorInt> MapVectorInt;
int main() {
MapVectorInt myMap;
VectorInt v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
VectorInt v2;
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
myMap.insert(std::make_pair(0, v1));
myMap.insert(std::make_pair(1, v2));
return 0;
}
What is the bese way to take (extract/get) the vector at position x (eg 1,2...);
You can use:
auto v1=myMap[0]; //This will do a deep copy for the vector
or:
auto& v1=myMap[0]; //This will hold a reference to the vector
Be careful in the second approach because if the item has removed , the reference will be invalid.
If you want a safe option use std::map::at:
auto v1=myMap.at(0);
or:
auto& v1=myMap.at(0)
This will throw an exception (std::out_of_range) if the item is not there.
Short answer:
Best place to look for it http://en.cppreference.com/w/cpp/container/map/find
With context:
For visualisation only, I guess you should express you intention as you said in your question(get an element from the map and not "eventually" insert it as using [] for accessing a non-existing element creates it), so:
const auto& search = myMap.find(1);
if (search != example.end()) {
for(auto value: search->second) {
std::cout << value << '\n';
}
}
else {
std::cout << "Not found\n";
}
Try It Live
You can refer to the vector in the map with myMap[0] or myMap[1].
You could make an alias for either of those, e.g. auto& x = myMap[0];. Using the alias will access the vector as it is stored in the map.
To take a copy of the vector (so that you can modify the map contents without changing the copy), it is just the same as copying any other object:
auto x = myMap[0];
Note that 0 and 1 refer to the key you used in the insert call; not to the search order or the insertion order (although in your example those things all happen to be the same).

The pair library (of the STL) could not be found [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing a C++ program using iterators. I have a data structure that is a map. I use iterators to loop from the begin to the end of the map and for each element of the map I do something with the key and the value.
So when I want to know the key and the value of a specific element of the map, I use first() and second() on my iterator.
Like this :
#include <map>
#include <pair>
map<unsigned long, int> myMap;
map<unsigned long, int>::const_iterator it;
for(it = myMap.cbegin(); it != myMap.cend(); ++it)
{
unsigned long key_of_map = it.first();
int val = it.second();
cout << "Key is : " << key_of_map << endl << "Value is : " << val << endl;
}
When I compile it, it tells me :
"../src/myfile.cpp:16:10: fatal error: 'pair' file not found"
I'm using Eclipse (version Luna), the standard version I downloaded from the official website (I didn't changed anything).
You need to
#include <utility>
to use std::pair<>

Trying to create a function that will join words together from two unevenly sized vectors of strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
If I have these two vectors:
vec1: "hello", "world"
vec2: "it", "is", "sunny", "today"
resultvector: "helloit", "worldis"
I need to use stl for this, and a functor. So far what I have throws a stackdump error:
My functor:
reads in two std strings, and "+" them together, returns result of operation.
My function:
Creates a std::list list, and uses std::transform(vec1.begin(), vec1.end(), vec2.begin(), list.begin(), functor()); return list;
My suspicion is that I don't know how to make it iterate only till the end of smaller container, and also maybe I am doing something funky with list.begin() and need something else for it.
Any ideas on how I might accomplish this?
Note: the two vectors are std::vector<string> and result is std::list<string>
Thank you in advance for your help!
In your transform call, use back_inserter(list) in place of list.begin(). back_inserter produces an iterator that translates assignments into push_back calls on underlying container.
std::transform(
vec1.begin(),
vec1.begin()+std::min(vec1.size(),vec2.size()),
vec2.begin(),
std::back_inserter(list),
functor()
);
#include<iostream>
#include<algorithm>
using namespace std;
struct ff
{
string operator ()(const string& x, const string& y ){
return x+y; }
};
int main ()
{
vector <string> vec1{"hello", "world"};
vector <string> vec2{ "it", "is", "sunny", "today"};
vector <string> resultvector;
std::transform(
vec1.begin(), vec1.begin()+min(vec1.size(),vec2.size()),
vec2.begin(),
back_inserter(resultvector),
ff()
);
for(auto i:resultvector)
cout<<i<<endl;
}