Topic: std::vector construction question
Author: Robert Klemme <babelfish@gmx.net>
Date: 2000/03/01 Raw View
hi,
your main problem seems to be, that the iterator of a map does not yield
the elment on deref. instead you would have to use somthing like
iter->first (for the key) or iter->second. since your compiler cannot
read your mind, it is not possible the way you suggested. you have to
copy them manually.
Gabor Greif schrieb:
>
> I have the problem of moving data from a std::map to a std::vector to be
> able to sort it.
>
> #include <map>
> #include <vector>
> #include <string>
> using namespace std;
>
> ...
>
> void processmap(const map<string, int>& table)
> {
> vector<const string, int> v(table.begin(), table.end());
> sort(v.begin(), v.end(), SomePredicate);
> }
>
> I understand that the element type for both containers (pair<const string,
> int>) does not have a compiler generated assignment operator.
afaik this is wrong: there is an assignment for pair<A,B> for any A and
B that support operator=.
> The reason why I am posting is that of two compilers we use here one
> accepted the above example, the second choked on the vector constructor
> right from the start. A rewrite using push_back revealed the absence of the
> assignment operator.
strange...
robert
--
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra e 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/03/01 Raw View
On Wed, 1 Mar 2000 01:49:19 CST, Robert Klemme <babelfish@gmx.net>
wrote:
: Gabor Greif schrieb:
: >
: > I have the problem of moving data from a std::map to a std::vector to be
: > able to sort it.
: >
: > void processmap(const map<string, int>& table)
: > {
: > vector<const string, int> v(table.begin(), table.end());
Assuming typo: vector<pair<const string, int> >
: > sort(v.begin(), v.end(), SomePredicate);
: > }
: >
: > I understand that the element type for both containers (pair<const string,
: > int>) does not have a compiler generated assignment operator.
:
: afaik this is wrong: there is an assignment for pair<A,B> for any A and
: B that support operator=.
But, string const does not support assignment.
Would
vector<pair<string, int> > v;
solve the problem?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Gabor Greif" <gabor@no.netopia.com>
Date: 2000/02/29 Raw View
I have the problem of moving data from a std::map to a std::vector to be
able to sort it.
#include <map>
#include <vector>
#include <string>
using namespace std;
...
void processmap(const map<string, int>& table)
{
vector<const string, int> v(table.begin(), table.end());
sort(v.begin(), v.end(), SomePredicate);
}
I understand that the element type for both containers (pair<const string,
int>) does not have a compiler generated assignment operator.
So I am wondering whether the above function could ever compile.
The first problem I see is in the constructor of vector taking a pair of
input iterators.
Does the standard require that construction this way works even in absence
of the assignment operator for vector's element type?
Second, the sort function surely needs the presence of an assignment
operator. So the second line should never compile. Correct?
The reason why I am posting is that of two compilers we use here one
accepted the above example, the second choked on the vector constructor
right from the start. A rewrite using push_back revealed the absence of the
assignment operator.
Thanks in advance,
Gabor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]