How to get value from map by key - c++

How I can get value from map by key? I have a vector of ints A, and I have a map. I want to print M[1], but I don't understand how I can do it, because I've got an error:
error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'mapped_type' (aka 'std::__1::pair<int, int>'))
cout << M[1];
~~~~ ^ ~~~~
My code:
int main() {
vector<int> A;
map<int, pair<int,int> > M;
FOR(i,1,maxN) {
pair<int,int> p;
p.first = 1;
p.second = 2;
M[i] = p;
}
FOR(i,0,t) {
int x = A[i];
cout << M[x] << endl;
}
return 0;
}

Value type of the map is a std::pair. You need to individually print the 2 values in the pair:
cout<< M[x].first << "," << M[x].second << endl;

Paani has a good answer. I thought I would post mine, which is the same idea but slightly different.
Instead of printing the values from the pair you can create an ostream function to print any pair.
Like this:
#include <iostream>
#include <utility>
template<class T, class U>
std::ostream& operator<<(std::ostream &os, const std::pair<T, U> &p) {
os << '{' << p.first << ',' << p.second << '}';
return os;
}
int main() {
std::pair<int, int> p(7, 11);
std::pair<std::string, double> q("My Double", 37.02);
std::cout << p << std::endl;
std::cout << q << std::endl;
return 0;
}

Related

Can't compile on std::min_element's return

#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
std::map<double, double> A;
const auto it = std::min_element(A.begin(), A.end(),
[](decltype(A)::value_type& l, decltype(A)::value_type& r) -> bool { return l.second < r.second; });
std::cout << *it << std::endl;
}
I wish to compute the minimum in the map.
This code failed to compile. I thought the way to use std::min_element's returned iterator is by referencing it. No?
The error message on the std::cout line is "invalid operands to binary expression".
std::map::iterator::value_type (ie. the type of *it) is std::pair<const double,double>, and there is no standard overload of operator<<(std::ostream &, std::pair<const double,double>).
You could either define one, or do something like std::cout << it->first << ' ' << it->second << std::endl;.
The element type of std::map is a std::pair<const key_type, mapped_type>. *it will give you a reference to that. There is no output output operator defined for std::pair so the code fails to compile.
You will either have to add an overload for it like
std::ostream& operator <<(std::ostream& os, const std::pair<const double, double>& e)
{
return os << "{" << e.first << ", " << e.second << "}\n";
}
or just print what you want like
std::cout << it->first << " " << it->second << "\n";

How to print out a 2D vector of objects in C++?

I'm writing a program which includes a 2D vector of objects:
class Obj{
public:
int x;
string y;
};
vector<Obj> a(100);
vector< vector<Obj> > b(10);
I've stored some values of vector a in vector b.
I get an error when I try to print it out like this:
for(int i=0; i<b.size(); i++){
for(int j=0; j<b[i].size(); j++)
cout << b[i][j];
cout << endl;
}
error info:
D:\main.cpp:91: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and '__gnu_cxx::__alloc_traits >::value_type {aka Obj}')
cout << b[i][j];
^
Your problem is not related to vectors, it is related to sending object(s) of some user defined type Obj to the standard output. When you send an object to the output stream using the operator<< as you do with:
cout << b[i][j];
the stream does not know what to do with it as none of the 12 overloads accepts a user defined type Obj. You need to overload the operator<< for your class Obj:
std::ostream& operator<<(std::ostream& os, const Obj& obj) {
os << obj.x << ' ' << obj.y;
return os;
}
or even a vector of Objs:
std::ostream& operator<<(std::ostream& os, const std::vector<Obj>& vec) {
for (auto& el : vec) {
os << el.x << ' ' << el.y << " ";
}
return os;
}
For more info on the subject check out this SO post:
What are the basic rules and idioms for operator overloading?
This has nothing to do with your vectors.
You are trying to print an Obj, but you didn't tell your computer how you would like it to do that.
Either print b[i][j].x and b[i][j].y individually, or overload operator<< for Obj.
There is no cout::operator<< that takes a class Obj as a right hand side. You could define one. Simplest solution is to send x and y to cout separately. But use range-based for-loops!
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Obj {
public:
int x;
string y;
};
vector<vector<Obj>> b(10);
void store_some_values_in_b() {
for (auto& row : b) {
row.resize(10);
for (auto& val : row) {
val.x = 42; val.y = " (Ans.) ";
}
}
}
int main() {
store_some_values_in_b();
for (auto row: b) {
for (auto val : row) {
cout << val.x << " " << val.y;
}
cout << endl;
}
}
Maybe like below
for(int i=0; i<b.size(); i++){
for(int j=0; j<b[i].size(); j++)
cout << "(" << b[i][j].x << ", " << b[i][j].y << ") ";
cout << endl;
}

error: no match for ‘operator[]’?

I am trying to access the std::map data using key, but I am getting the error
error: no match for ‘operator[]’ (operand types are ‘const std::pair’ and ‘int’)
#include <map>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<map<int, double>> mainData;
for(int i = 0; i < 10; i++) {
map<int, double> data;
data[1] = i;
data[2] = i*2.0;
data[5] = i*7.2;
mainData.push_back(data);
}
for(auto& it1: mainData) {
for(auto& it2: it1) {
cout << it2.first << " " << it2.second << "\n";
cout << it2[5] << "\n"; // Error occurs here
}
cout << "\n";
}
return 0;
}
I think you have a typo here. I think you meant to access it1 instead of it2, because it1 is actually the map and it2 is the pair within the map.
cout << it1[5] << "\n";
Maybe you should use different variable names:
#include <map>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<map<int, double>> mainData;
for(int i = 0; i < 10; i++) {
map<int, double> data;
data[1] = i;
data[2] = i*2.0;
data[5] = i*7.2;
mainData.push_back(data);
}
for(auto& map: mainData) {
for(auto& pair: map) {
cout << pair.first << " " << pair.second << "\n";
cout << map[5] << "\n";
}
cout << "\n";
}
return 0;
}
Maybe you meant to use it1[5]?
it2 is a std::pair (http://www.cplusplus.com/reference/utility/pair/). You meant it1[5] maybe ? Actually doing it2[x] can't work then there is a problem in the code you made
because there are no [] operator for std pair, probably you mean it1[5] instead of it2[5]

Convert vector<string> to other types

I've a class withmap<string,vector<string>>.
I want to give a user a member function to receive the value for a key in different formats than std::vector(string):
vector(string),
string,
vector(int),
vector(float) and
bool
example:
bool x = (bool) myClass["dummy_boolean_type"]
x = myClass["dummy_boolean_type"].as<bool>
int y = (int) myClass["dummy_boolean_type"]
Can someone have an example what is the best way to achieve it ?
(without a use in Boost)
You cannot provide your class with any member function or functions
that will support the two constructions you want:
T x = myClassInstance["key"].as<T> // (1)
T x = (T) myClassInstance["key"] // (2)
In the case of (1), the immediate reason is simply that the construction is
not legal C++. So let's suppose it is replaced by:
T x = myClassInstance["key"].as<T>() // (1a)
But this doesn't help, and the reason is the same for both (1a) and (2):
The expression myClassInstance["key"] will have to evaluate to some object e or reference to such that is returned by:
myClass::operator[](std::string const &);
This e is the vector<string> to which key maps in your
map<string,vector<string> data member of myClass. It is not myClassInstance.
So what you are asking for are member functions of myClass that will support
the constructions:
T x = e.as<T> // (1b)
T x = (T) e // (2b)
where e is an std::vector<std::string>.
Clearly, nothing you can do in myClass can address your requirement. In
(1b) and (2b), myClass is nowhere to be seen.
Is there any template member function of std::vector<std::string>:
template<typename T>
T as() const;
that has the behaviour you want for (1b)?
Does std::vector<std::string> have any template member function:
template<typename T>
operator T() const;
that has the behaviour you want for (2b)?
No and No.
However...
You can implement an as template member function of myClass that
supports conversions such as you mention. Its signature - of course -
would be:
template<typename T>
T myClass::as(std::string const & key) const;
and you would invoke it like:
T x = myClassInstance.as<T>("key") // (1c)
I'll sketch an implemention that assumes we're content with the
conversions from std::vector<std::string> to:
std::string
std::vector<int>
bool
which will be enough to get you under way.
To convert an std::vector<std::string> vs to std::string I'll concatenate
the elements of vs.
To convert an std::vector<std::string> vs to std::vector<int> I'll convert each element of vs from a decimal numeral, if I can, to the
integer the numeral represents, and return a vector of those integers. Without
prejudice to other policies I'll throw an std::invalid_argument exception
when an element doesn't trim to a decimal numeral.
I am spoilt for choice as to what you might mean by converting an std::vector<std::string>
to bool, so I will arbitrarily say that vs is true if I can convert it
to a vector<int> of which any element is non-0 and is false if I can convert
it to a vector<int> in which all elements are 0.
The sketch:
#include <type_traits>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
namespace detail {
template<typename T>
struct convert_to
{
static T from(std::vector<std::string> const & vs) {
static constexpr bool always_false = !std::is_same<T,T>::value;
static_assert(always_false,
"Calling `convert_to<T>::from` for unimplemented `T`");
return *(T*)nullptr;
}
};
template<>
std::string convert_to<std::string>::from(std::vector<std::string> const & vs)
{
std::string s;
for ( auto const & e : vs ) {
s += e;
}
return s;
}
template<>
std::vector<int>
convert_to<std::vector<int>>::from(std::vector<std::string> const & vs)
{
auto lamb = [](std::string const & s) {
std::size_t lastoff = s.find_last_not_of(" \t\f\v\n\r");
int i;
try {
std::size_t nlen;
i = std::stoi(s,&nlen);
if (nlen <= lastoff) {
throw std::invalid_argument("");
}
}
catch(std::invalid_argument const & e) {
throw std::invalid_argument(
"Cannot convert \"" + s + "\" to int");
}
return i;
};
std::vector<int> vi;
std::transform(vs.begin(),vs.end(),std::back_inserter(vi),lamb);
return vi;
}
template<>
bool convert_to<bool>::from(std::vector<std::string> const & vs)
{
auto vi = convert_to<std::vector<int>>::from(vs);
for (auto const & i : vi) {
if (i) {
return true;
}
}
return false;
}
} // namespace detail
struct myClass // Your class
{
// Whatever...
std::vector<std::string> & operator[](std::string const & key) {
return _map[key];
}
template<typename T>
T as(std::string const & key) {
return detail::convert_to<T>::from(_map[key]);
}
// Whatever...
private:
std::map<std::string,std::vector<std::string>> _map;
};
The one take-away point here is the use of template<typename T>
detail::struct convert_to, with its solitary static member function:
T from(std::vector<std::string> const & vs)
which in the default instantiation will provoke a static_assert failure
reporting that no conversion to T from std::vector<std::string> has been
defined.
Then, for each type U to which you want a conversion, you have just to
write a specializing definition:
template<>
U convert_to<U>::from(std::vector<std::string> const & vs);
as you see fit, and the construction (1c) will use it as per:
template<typename T>
T myClass::as(std::string const & key) {
return detail::convert_to<T>::from(_map[key]);
}
Here's an illustrative progam you can append to the sketch:
#include <iostream>
using namespace std;
template<typename T>
static void print_vec(std::vector<T> const & v)
{
cout << "{ ";
for (auto const & e : v) {
cout << e << " ";
}
cout << "}\n";
}
static void print_vec(std::vector<std::string> const & v)
{
cout << "{ ";
for (auto const & e : v) {
cout << '\"' << e << "\" ";
}
cout << "}\n";
}
int main()
{
myClass f;
f["int_vec"] = vector<string>{"0","1 "," 2"};
cout << "f[\"int_vec\"] = "; print_vec(f["int_vec"]);
f["true_vec"] = vector<string>{"0"," 1 ","0"};
cout << "f[\"true_vec\"] = "; print_vec(f["true_vec"]);
f["false_vec"] = vector<string>{"0"," 0","0 "};
cout << "f[\"false_vec\"] = "; print_vec(f["false_vec"]);
f["not_int_vec0"] = vector<string>{"0","1","2",""};
cout << "f[\"not_int_vec0\"] = "; print_vec(f["not_int_vec0"]);
f["not_int_vec1"] = vector<string>{"0","#","2",};
cout << "f[\"not_int_vec1\"] = "; print_vec(f["not_int_vec1"]);
f["not_int_vec2"] = vector<string>{"0"," 1$","2",};
cout << "f[\"not_int_vec2\"] = "; print_vec(f["not_int_vec2"]);
cout << "f.as<string>(\"int_vec\") = \""
<< f.as<string>("int_vec") << '\"' << endl;
cout << "f.as<string>(\"true_vec\") = \""
<< f.as<string>("true_vec") << '\"' << endl;
cout << "f.as<string>(\"false_vec\") = \""
<< f.as<string>("false_vec") << '\"' << endl;
cout << "f.as<string>(\"not_int_vec0\") = \""
<< f.as<string>("not_int_vec0") << '\"' << endl;
cout << "f.as<string>(\"not_int_vec1\") = \""
<< f.as<string>("not_int_vec1") << '\"' << endl;
cout << "f.as<string>(\"not_int_vec2\") = \""
<< f.as<string>("not_int_vec2") << '\"' << endl;
vector<int> va = f.as<vector<int>>("int_vec");
cout << "f.as<vector<int>>(\"int_vec\") = ";
print_vec(f.as<vector<int>>("int_vec"));
cout << boolalpha << "f.as<bool>(\"true_vec\") = "
<< f.as<bool>("true_vec") << endl;
cout << boolalpha << "f.as<bool>(\"false_vec\") = "
<< f.as<bool>("false_vec") << endl;
try {
cout << "f.as<vector<int>>(\"not_int_vec0\")...";
auto b = f.as<vector<int>>("not_int_vec0");
(void)b;
}
catch(std::invalid_argument const & e) {
cout << e.what() << endl;
}
try {
cout << "f.as<vector<int>>(\"not_int_vec1\")...";
auto b = f.as<vector<int>>("not_int_vec1");
(void)b;
}
catch(std::invalid_argument const & e) {
cout << e.what() << endl;
}
try {
cout << "f.as<vector<int>>(\"not_int_vec2\")...";
auto b = f.as<vector<int>>("not_int_vec2");
(void)b;
}
catch(std::invalid_argument const & e) {
cout << e.what() << endl;
}
// char ch = f.as<char>("int_vec"); <- static_assert fails
return 0;
}
It outputs:
f["int_vec"] = { "0" "1 " " 2" }
f["true_vec"] = { "0" " 1 " "0" }
f["false_vec"] = { "0" " 0" "0 " }
f["not_int_vec0"] = { "0" "1" "2" "" }
f["not_int_vec1"] = { "0" "#" "2" }
f["not_int_vec2"] = { "0" " 1$" "2" }
f.as<string>("int_vec") = "01 2"
f.as<string>("true_vec") = "0 1 0"
f.as<string>("false_vec") = "0 00 "
f.as<string>("not_int_vec0") = "012"
f.as<string>("not_int_vec1") = "0#2"
f.as<string>("not_int_vec2") = "0 1$2"
f.as<vector<int>>("int_vec") = { 0 1 2 }
f.as<bool>("true_vec") = true
f.as<bool>("false_vec") = false
f.as<vector<int>>("not_int_vec0")...Cannot convert "" to int
f.as<vector<int>>("not_int_vec1")...Cannot convert "#" to int
f.as<vector<int>>("not_int_vec2")...Cannot convert " 1$" to int
(gcc 5.1, clang 3.6, C++11)

Iterate through a vector column C++

I am wanting to iterate through my entire column that contains the type double and divide each value by the size of the column. This will give me the frequency. I would have not issue doing this in an array or any other datatype. I am still learning about the vectors. Here is the 2D vector type that I am trying to manipulate
vector<pair<char, double>> output;
If you have C++11:
for (auto & p : output)
{
cout << p.first << " " << p.second << "\n";
}
std::cout << std::endl;
or with C++03
for (std::vector<std::pair<char, double> >::iterator it = output.begin();
it != output.end(); ++it)
{
cout << it .first << " " << it .second << "\n";
}
std::cout << std::endl;
You can treat a vector just like an array, and access the elements using []. For example:
for (size_t i = 0; i < output.size(); ++i)
{
pair<char, double>& element = output[i]; // access element i in the vector
cout << element.first; // prints the first member of the pair
cout << element.second; // prints the second member of the pair
}
std::pair is a struct template that provides a way to store two
heterogeneous objects as a single unit.
if you have a vector of pairs this means you will access data through pair interface . You can get first data of pair via first member and second via second.
for(std::vector<std::pair <char, double> >::const_iterator vpci = arg.begin();
vpci != arg.end(); ++vpci) {
cout << vpci->first << "->" << vpci->second;
}
or even better, maybe create a template:
template <typename T1, typename T2>
void prn_vecOfPair(const std::vector<std::pair <T1, T2> > &arg, string sep ="") {
for(std::vector<std::pair <T1, T2> >::const_iterator vpci = arg.begin();
vpci != arg.end(); ++vpci) {
cout << vpci->first << "->" << vpci->second << sep;
}
}
in C++11 this (as usual) can be done much easier:
for (auto & i : output)
{
cout << i->first << "->" << i->second << "\n";
}