why the iteration in the code below doesn't work ? I mean the iterator is incremented but evaluating this expression o!=RateCurve.end() always give true.
i need such a function because i'm using it in a wrapper of a map to contruct an interest rate curve.
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <map>
#include <exception>
#include <vector>
#include <stdio.h>
using namespace std;
map<double, double>::iterator getIt(map<double, double> o){
return o.begin();
}
int main ()
{
map<double, double> RateCurve;
RateCurve[3.3 ]=0.034 ;
RateCurve[1.2 ]=0.03 ;
RateCurve[0.2 ]=.001 ;
RateCurve[6.1 ]=.023 ;
map<double, double>::iterator o=getIt(RateCurve);
while (o!=RateCurve.end()){
cout << "RateCurve[" << o->first << "] = " << o->second << endl;
++o;
}
}
getIt(map<double, double> o)
You copy the map, so the iterator returned points into an entirely unrelated map to the one you wanted. Worse, the copy gets destroyed at the end of the function call, and the iterator you try to use is no longer valid, so you have Undefined Behaviour.
Make getIt take a reference, and since you don't actually change the elements, you can make it const too. Then you need to change the return type:
std::map<double, double>::const_iterator
getIt(map<double, double> const& o)
{
return o.begin();
}
Also, please reconsider your use of bad practices using namespace std; and endl.
Related
I have an input of:
1 a
2 b
..
I would like to insert them to a vector of pairs, with copy function, like this:
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
int main(void) {
std::vector<std::pair<int, char>> v;
std::copy(std::istream_iterator<std::pair<int, char>>(std::cin), std::istream_iterator<std::pair<int, char>>(), std::back_inserter(v));
for(auto pair: v)
std::cout << pair.first << std::endl;
return 0;
}
However, this will not compile: error: no match for 'operator>>', since it probably needs an operator overloading.
Does that mean that I will have to create my own class, which inhertis from std::vector, and then overload the operator?
I would like to avoid using my own class, instead of the standard vector class.
The problem is not with std::vector, it's std::istream_iterator. The reason being that std::pair doesn't have a deserialization operator defined.
You can still use std::vector and std::back_insert_iterator, but you'll need to define your own input iterator. One that reads pairs of values.
Some people may suggest that you define operator>> for your pairs, but that is an unreliable technique. It will depend on the operator being defined before you include <algorithm> and <iterator>.
You could copy it through a proxy object:
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
struct proxy
{
friend auto operator>>(std::istream& is, proxy& prox) -> std::istream&
{
is >> std::get<0>(prox.target);
is >> std::get<1>(prox.target);
return is;
}
operator std::pair<int, char>() const {
return target;
}
std::pair<int, char> target;
};
int main(void) {
std::vector<std::pair<int, char>> v;
std::copy(std::istream_iterator<proxy>(std::cin),
std::istream_iterator<proxy>(),
std::back_inserter(v));
for(auto pair: v)
std::cout << pair.first << std::endl;
return 0;
}
I have a program in which I am using an iterator to get access to a value of a map entry .
#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <utility>
#include <algorithm>
using namespace std;
int main()
{
int a[]={1,2,4,7,9,3,3,55,66,88,11};
int k = 58;
unordered_map<int,int> c;
int i=0;
for(auto x: a)
{
c.insert(make_pair(x,i));
i++;
}
for(auto x: c)
{
if(c.count(k-x.first)==1) printf("%d %d\n",x.second,(c.find(k-x))->second);
}
}
In the line:
if(c.count(k-x.first)==1) printf("%d %d\n",x.second,(c.find(k-x))->second);
I am getting errors especially in c.find()->second. I think it is the way to access elements from the iterator. What is wrong?
You cannot say
c.find(k-x)
Because k is an int and x is a std::pair<int, int>. Do you mean one of these two?
c.find(k - x.first)
Or
c.find(k - x.second)
As a side note, it is generally unsafe to directly derefernce the returned iterator from find because if find returns .end() (as in, it didn't find an element with that key) then you've got a problem.
I am currently trying to write a list of pairs. my code is :
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;
list<pair<string,char>> listPair;
list<pair<string,char>>::iterator it;
void printStars(list<pair<string,char>> listPair)
{
for (it=listPair.begin(); it != listPair.end(); it++)
cout << it->first <<" ";
cout << endl;
}
int main()
{
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), printStars);
return 0;
}
Compilation fails with:
error C2664: 'void (std::list<_Ty>)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::list<_Ty>'
Can you please help me detect where exactly is the problem?
The functor you pass to std::for_each is expected to accept an element of the range you pass into std::for_each. Your last has pair<string,char> elements, so your functor should have a signature like: void printStars(const pair<string,char>& elem).
In addition, to pass a plain function to std::for_each you need to use std::ref or (on an old compiler) std::ptr_fun.
#include <iostream>
#include <algorithm>
#include <list>
#include <string> // missing include
#include <utility>
#include <functional>
using namespace std;
typedef list< pair<string,char> > list_t;
list_t listPair;
void printStars(list_t::reference x) // use a reference, otherwise you create a copy
{
cout << x.first << " " << x.second << endl;
}
int main()
{
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), std::ref(printStars)); // C++11
for_each(listPair.begin(), listPair.end(), std::ptr_fun(&printStars)); // C++98
return 0;
}
Your problem is, your printStars() expects a list, however for_each passes it each item, not the actual list:
Working code :
#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
#include <string>
#include <utility>
list<pair<string,char> > listPair;
list<pair<string,char> >::iterator it;
void printStars(const pair<string,char> & listPair){ //notice the &, so it would pass by reference and not make a new copy of the pair.
cout << listPair.first << ' ';
}
int main() {
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), printStars);
cout << endl;
return 0;
}
Below is a slightly modified code from one good example how to copy values fro one vector of strings to another vector of objects. (see: another copy algorithm )
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;
class Object
{
public:
std::string Name;
Object(const std::string& Name_ = "")
: Name(Name_)
{
}
};
int main()
{
//std::vector<Object> objects(3, Object());
std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());
std::vector<std::string> names;
names.push_back("Alpha");
names.push_back("Beta");
names.push_back("Gamma");
std::vector<std::string>::const_iterator names_it;
names_it = static_cast<const std::vector<std::string>&>(names).begin();
//std::for_each(objects.begin(), objects.end(), bind(&Object::Name, _1) = *var(names_it)++);
std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);
//std::vector<Object>::iterator it, end = objects.end();
std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
for (it = objects.begin(); it != end; ++it) {
//std::cout << it->Name << std::endl;
std::cout << (*it)->Name << std::endl;
}
return EXIT_SUCCESS;
}
In this case I'm using dynamically allocated Objects, and because boost::lambda::bind can't handle such changes as boost::bind do, I need to dereference placeholder in order to compile:
std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);
But then at the output I've got:
Gamma
Gamma
Gamma
What is your explanation?
std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());
This is equivalent to doing:
shared_ptr<Object> object = make_shared<Object>();
std::vector<shared_ptr<Object> > objects(3, object);
The vector constructor will then make 3 copies of that object pointer, which will all refer to same one and unique Object (that is to say, *object). Fill the vector with separate pointers each initialized with their own Object.
typedef map<string,int> mapType;
mapType::const_iterator i;
i = find_if( d.begin(), d.end(), isalnum );
at the '=' i am getting the error:
Error:no operator "=" matches these operands
I know that find_if returns an iterator once the pred resolves to true, so what is the deal?
The documentation for std::find_if
We can only guess at the error as you have only provided half the problem.
Assuming d is mapType and the correct version of isalnum
The problem is that the functor is being passed an object to mapType::value_type (which is how the map and all containers store their value). For map the value_type is actually a key/value pair actually implemented as std::pair<Key,Value>. So you need to get the second part of the object to test with isalnum().
Here I have wrapped that translation inside another functor isAlphaNumFromMap that can be used by find_if
#include <map>
#include <string>
#include <algorithm>
// Using ctype.h brings the C functions into the global namespace
// If you use cctype instead it brings them into the std namespace
// Note: They may be n both namespaces according to the new standard.
#include <ctype.h>
typedef std::map<std::string,int> mapType;
struct isAlphaNumFromMap
{
bool operator()(mapType::value_type const& v) const
{
return ::isalnum(v.second);
}
};
int main()
{
mapType::const_iterator i;
mapType d;
i = std::find_if( d.begin(), d.end(), isAlphaNumFromMap() );
}
If d is a map, then the problem is with your attempted use of isalnum.
isalnum takes a single int parameter, but the predicate called by find_if receives a map::value_type. The types are not compatible, so you need something that adapts find_if to `isalnum. Such as this:
#include <cstdlib>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
typedef map<string,int> mapType;
bool is_alnum(mapType::value_type v)
{
return 0 != isalnum(v.second);
}
int main()
{
mapType::const_iterator i;
mapType d;
i = find_if( d.begin(), d.end(), is_alnum );
}