Why is there a syntax error in my map iterator?Where and why do I have to put the “template” and “typename” keywords?How do I iterate over the words of a string?Why can templates only be implemented in the header file?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?Why are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?Use std container iterator in template class
When to use и or а as “and”?
How do I type a hyphen in iOS 12?
Dependent voltage/current sources
What class is best to play when a level behind the rest of the party?
Why is it bad to use your whole foot in rock climbing
Is all-caps blackletter no longer taboo?
Print "N NE E SE S SW W NW"
Does a single fopen introduce TOCTOU vulnerability?
How can powerful telekinesis avoid violating Newton's 3rd Law?
Nth term of Van Eck Sequence
How to make a composition of functions prettier?
What do I need to do, tax-wise, for a sudden windfall?
How to use the word seem
What is the theme of analysis?
How many sets of dice do I need for D&D?
Quasar Redshifts
What did the 8086 (and 8088) do upon encountering an illegal instruction?
Convert GE Load Center to main breaker
Finding diameter of a circle using two chords and angle between them
How much web presence should I have?
Is Lambda Calculus purely syntactic?
What's the best way to quit a job mostly because of money?
Why did the World Bank set the global poverty line at $1.90?
Linked novellas where humans are engineered to adapt to a variety of environments
Why is there a syntax error in my map iterator?
Where and why do I have to put the “template” and “typename” keywords?How do I iterate over the words of a string?Why can templates only be implemented in the header file?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?Why are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?Use std container iterator in template class
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I wrote a template function for flipping my std::map
keys and values.
#include <map>
#include <iterator>
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
VS gives me a syntax error:
unexpected token 'identifier', expected ';'
I don't know what I'm doing wrong.
c++
add a comment |
I wrote a template function for flipping my std::map
keys and values.
#include <map>
#include <iterator>
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
VS gives me a syntax error:
unexpected token 'identifier', expected ';'
I don't know what I'm doing wrong.
c++
1
Remember,auto
is your friend. Let it do the work for you.for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
->for (auto it = src.begin(); it != src.end(); ++it)
or better yetfor (auto e : dst)
– NathanOliver
Mar 24 at 23:21
@NathanOliver Or, better yet,for (auto e : src)
– Lightness Races in Orbit
Mar 24 at 23:23
The best one is: for (auto&[key, value] : src) ...
– Dmitry
Mar 24 at 23:24
add a comment |
I wrote a template function for flipping my std::map
keys and values.
#include <map>
#include <iterator>
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
VS gives me a syntax error:
unexpected token 'identifier', expected ';'
I don't know what I'm doing wrong.
c++
I wrote a template function for flipping my std::map
keys and values.
#include <map>
#include <iterator>
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
VS gives me a syntax error:
unexpected token 'identifier', expected ';'
I don't know what I'm doing wrong.
c++
c++
edited Mar 25 at 0:38
Remy Lebeau
351k19277474
351k19277474
asked Mar 24 at 23:14
MossiMossi
81
81
1
Remember,auto
is your friend. Let it do the work for you.for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
->for (auto it = src.begin(); it != src.end(); ++it)
or better yetfor (auto e : dst)
– NathanOliver
Mar 24 at 23:21
@NathanOliver Or, better yet,for (auto e : src)
– Lightness Races in Orbit
Mar 24 at 23:23
The best one is: for (auto&[key, value] : src) ...
– Dmitry
Mar 24 at 23:24
add a comment |
1
Remember,auto
is your friend. Let it do the work for you.for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
->for (auto it = src.begin(); it != src.end(); ++it)
or better yetfor (auto e : dst)
– NathanOliver
Mar 24 at 23:21
@NathanOliver Or, better yet,for (auto e : src)
– Lightness Races in Orbit
Mar 24 at 23:23
The best one is: for (auto&[key, value] : src) ...
– Dmitry
Mar 24 at 23:24
1
1
Remember,
auto
is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
-> for (auto it = src.begin(); it != src.end(); ++it)
or better yet for (auto e : dst)
– NathanOliver
Mar 24 at 23:21
Remember,
auto
is your friend. Let it do the work for you. for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
-> for (auto it = src.begin(); it != src.end(); ++it)
or better yet for (auto e : dst)
– NathanOliver
Mar 24 at 23:21
@NathanOliver Or, better yet,
for (auto e : src)
– Lightness Races in Orbit
Mar 24 at 23:23
@NathanOliver Or, better yet,
for (auto e : src)
– Lightness Races in Orbit
Mar 24 at 23:23
The best one is: for (auto&[key, value] : src) ...
– Dmitry
Mar 24 at 23:24
The best one is: for (auto&[key, value] : src) ...
– Dmitry
Mar 24 at 23:24
add a comment |
2 Answers
2
active
oldest
votes
GCC tells you more clearly what's wrong: you need typename
there, for arcane C++ reasons.
Like this:
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^
Further reading:
- Where and why do I have to put the "template" and "typename" keywords?
I'd instead write it like this:
#include <map>
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)
std::map<B, A> dst;
for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));
return dst;
In fact, as it happens, I did; two weeks ago. :)
(You could also consider some .emplace
and std::move
etc, depending on what A
and B
are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
add a comment |
The reason is that std::map<A,B>::iterator
is a dependent name (in rough terms, it is in a templated function, and depends on A
and B
which are parameters of that template). So it needs to be preceded with the typename
keyword.
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
Additionally you would probably be better specifying src
as const
, and using const_iterator
rather than iterator
, viz
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
or (C++11 and later), let the compiler do the type deduction for you by using auto
and std::make_pair
. This avoids need for the programmer to worry about dependent names.
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (const auto &e : src)
dst.insert(std::make_pair(e.second, e.first));
return dst;
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329483%2fwhy-is-there-a-syntax-error-in-my-map-iterator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
GCC tells you more clearly what's wrong: you need typename
there, for arcane C++ reasons.
Like this:
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^
Further reading:
- Where and why do I have to put the "template" and "typename" keywords?
I'd instead write it like this:
#include <map>
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)
std::map<B, A> dst;
for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));
return dst;
In fact, as it happens, I did; two weeks ago. :)
(You could also consider some .emplace
and std::move
etc, depending on what A
and B
are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
add a comment |
GCC tells you more clearly what's wrong: you need typename
there, for arcane C++ reasons.
Like this:
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^
Further reading:
- Where and why do I have to put the "template" and "typename" keywords?
I'd instead write it like this:
#include <map>
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)
std::map<B, A> dst;
for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));
return dst;
In fact, as it happens, I did; two weeks ago. :)
(You could also consider some .emplace
and std::move
etc, depending on what A
and B
are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
add a comment |
GCC tells you more clearly what's wrong: you need typename
there, for arcane C++ reasons.
Like this:
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^
Further reading:
- Where and why do I have to put the "template" and "typename" keywords?
I'd instead write it like this:
#include <map>
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)
std::map<B, A> dst;
for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));
return dst;
In fact, as it happens, I did; two weeks ago. :)
(You could also consider some .emplace
and std::move
etc, depending on what A
and B
are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)
GCC tells you more clearly what's wrong: you need typename
there, for arcane C++ reasons.
Like this:
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
// ^^^^^^^^
Further reading:
- Where and why do I have to put the "template" and "typename" keywords?
I'd instead write it like this:
#include <map>
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B>& src)
std::map<B, A> dst;
for (const auto& p : src)
dst.insert(std::make_pair(p.second, p.first));
return dst;
In fact, as it happens, I did; two weeks ago. :)
(You could also consider some .emplace
and std::move
etc, depending on what A
and B
are likely to be, though since you cannot move from a map key this is only ever going to be "sort of useful".)
answered Mar 24 at 23:17
Lightness Races in OrbitLightness Races in Orbit
303k56491845
303k56491845
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
add a comment |
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
Thank you man. nice answer.
– Mossi
Mar 25 at 14:56
add a comment |
The reason is that std::map<A,B>::iterator
is a dependent name (in rough terms, it is in a templated function, and depends on A
and B
which are parameters of that template). So it needs to be preceded with the typename
keyword.
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
Additionally you would probably be better specifying src
as const
, and using const_iterator
rather than iterator
, viz
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
or (C++11 and later), let the compiler do the type deduction for you by using auto
and std::make_pair
. This avoids need for the programmer to worry about dependent names.
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (const auto &e : src)
dst.insert(std::make_pair(e.second, e.first));
return dst;
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
add a comment |
The reason is that std::map<A,B>::iterator
is a dependent name (in rough terms, it is in a templated function, and depends on A
and B
which are parameters of that template). So it needs to be preceded with the typename
keyword.
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
Additionally you would probably be better specifying src
as const
, and using const_iterator
rather than iterator
, viz
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
or (C++11 and later), let the compiler do the type deduction for you by using auto
and std::make_pair
. This avoids need for the programmer to worry about dependent names.
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (const auto &e : src)
dst.insert(std::make_pair(e.second, e.first));
return dst;
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
add a comment |
The reason is that std::map<A,B>::iterator
is a dependent name (in rough terms, it is in a templated function, and depends on A
and B
which are parameters of that template). So it needs to be preceded with the typename
keyword.
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
Additionally you would probably be better specifying src
as const
, and using const_iterator
rather than iterator
, viz
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
or (C++11 and later), let the compiler do the type deduction for you by using auto
and std::make_pair
. This avoids need for the programmer to worry about dependent names.
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (const auto &e : src)
dst.insert(std::make_pair(e.second, e.first));
return dst;
The reason is that std::map<A,B>::iterator
is a dependent name (in rough terms, it is in a templated function, and depends on A
and B
which are parameters of that template). So it needs to be preceded with the typename
keyword.
template <typename A, typename B>
std::map<B, A> flip_map(std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
Additionally you would probably be better specifying src
as const
, and using const_iterator
rather than iterator
, viz
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (typename std::map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<B, A>(it->second, it->first));
return dst;
or (C++11 and later), let the compiler do the type deduction for you by using auto
and std::make_pair
. This avoids need for the programmer to worry about dependent names.
template <typename A, typename B>
std::map<B, A> flip_map(const std::map<A, B> &src)
std::map<B, A> dst;
for (const auto &e : src)
dst.insert(std::make_pair(e.second, e.first));
return dst;
answered Mar 25 at 0:02
PeterPeter
28.5k32257
28.5k32257
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
add a comment |
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
Thank you, it's a complete answer.
– Mossi
Mar 25 at 14:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329483%2fwhy-is-there-a-syntax-error-in-my-map-iterator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Remember,
auto
is your friend. Let it do the work for you.for (std::map<A, B>::iterator it = src.begin(); it != src.end(); ++it)
->for (auto it = src.begin(); it != src.end(); ++it)
or better yetfor (auto e : dst)
– NathanOliver
Mar 24 at 23:21
@NathanOliver Or, better yet,
for (auto e : src)
– Lightness Races in Orbit
Mar 24 at 23:23
The best one is: for (auto&[key, value] : src) ...
– Dmitry
Mar 24 at 23:24