Why can't I do std::map.begin() + 1? The Next CEO of Stack OverflowQuery regarding C++ map iteratorWhy can't variables be declared in a switch statement?Concatenating two std::vectorsWhy can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?STL list and iteratorsWhy are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?std::getline does not accept std::back_insert_iteratorWhy is it faster to process a sorted array than an unsorted array?Could not deduce template argument for 'const' … from int
Why isn't the Mueller report being released completely and unredacted?
What is the value of α and β in a triangle?
Method for adding error messages to a dictionary given a key
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?
What flight has the highest ratio of time difference to flight time?
Why is quantifier elimination desirable for a given theory?
Can MTA send mail via a relay without being told so?
How to count occurrences of text in a file?
Writing differences on a blackboard
I want to delete every two lines after 3rd lines in file contain very large number of lines :
RigExpert AA-35 - Interpreting The Information
Which one is the true statement?
Is it okay to majorly distort historical facts while writing a fiction story?
How to avoid supervisors with prejudiced views?
Should I tutor a student who I know has cheated on their homework?
When you upcast Blindness/Deafness, do all targets suffer the same effect?
Is it ever safe to open a suspicious HTML file (e.g. email attachment)?
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
Bartok - Syncopation (1): Meaning of notes in between Grand Staff
Is it convenient to ask the journal's editor for two additional days to complete a review?
The exact meaning of 'Mom made me a sandwich'
Why do remote US companies require working in the US?
Why didn't Khan get resurrected in the Genesis Explosion?
Prepend last line of stdin to entire stdin
Why can't I do std::map.begin() + 1?
The Next CEO of Stack OverflowQuery regarding C++ map iteratorWhy can't variables be declared in a switch statement?Concatenating two std::vectorsWhy can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?STL list and iteratorsWhy are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?std::getline does not accept std::back_insert_iteratorWhy is it faster to process a sorted array than an unsorted array?Could not deduce template argument for 'const' … from int
I have a std::map, which I want to iterate over starting at the second entry.
I can workaround this fine, but I'm confused about why the "obvious" syntax doesn't compile. The error message doesn't help because it refers to std::string, which I'm not using here.
Here's some code
// suppose I have some map ...
std::map<int, int> pSomeMap;
// this is fine ...
std::map<int, int>::const_iterator pIterOne = pSomeMap.begin();
++pIterOne;
// this doesn't compile ...
std::map<int, int>::const_iterator pIterTwo = pSomeMap.begin() + 1;
VS2012 gives the following error on the above line
error C2784: 'std::_String_iterator<_Mystr> std::operator +
(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' :
could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'int'
Can anyone explain what's happening here?
c++ visual-c++ visual-studio-2012 stl
add a comment |
I have a std::map, which I want to iterate over starting at the second entry.
I can workaround this fine, but I'm confused about why the "obvious" syntax doesn't compile. The error message doesn't help because it refers to std::string, which I'm not using here.
Here's some code
// suppose I have some map ...
std::map<int, int> pSomeMap;
// this is fine ...
std::map<int, int>::const_iterator pIterOne = pSomeMap.begin();
++pIterOne;
// this doesn't compile ...
std::map<int, int>::const_iterator pIterTwo = pSomeMap.begin() + 1;
VS2012 gives the following error on the above line
error C2784: 'std::_String_iterator<_Mystr> std::operator +
(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' :
could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'int'
Can anyone explain what's happening here?
c++ visual-c++ visual-studio-2012 stl
1
Most bizarre error message ever?
– curiousguy
Jul 5 '13 at 9:11
@NathanOliver: If you're editing posts to move error messages from quotes to code blocks, may I suggest adding<!-- language: lang-none -->to disable syntax highlighting?
– Cerbrus
Mar 12 at 9:01
add a comment |
I have a std::map, which I want to iterate over starting at the second entry.
I can workaround this fine, but I'm confused about why the "obvious" syntax doesn't compile. The error message doesn't help because it refers to std::string, which I'm not using here.
Here's some code
// suppose I have some map ...
std::map<int, int> pSomeMap;
// this is fine ...
std::map<int, int>::const_iterator pIterOne = pSomeMap.begin();
++pIterOne;
// this doesn't compile ...
std::map<int, int>::const_iterator pIterTwo = pSomeMap.begin() + 1;
VS2012 gives the following error on the above line
error C2784: 'std::_String_iterator<_Mystr> std::operator +
(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' :
could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'int'
Can anyone explain what's happening here?
c++ visual-c++ visual-studio-2012 stl
I have a std::map, which I want to iterate over starting at the second entry.
I can workaround this fine, but I'm confused about why the "obvious" syntax doesn't compile. The error message doesn't help because it refers to std::string, which I'm not using here.
Here's some code
// suppose I have some map ...
std::map<int, int> pSomeMap;
// this is fine ...
std::map<int, int>::const_iterator pIterOne = pSomeMap.begin();
++pIterOne;
// this doesn't compile ...
std::map<int, int>::const_iterator pIterTwo = pSomeMap.begin() + 1;
VS2012 gives the following error on the above line
error C2784: 'std::_String_iterator<_Mystr> std::operator +
(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' :
could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'int'
Can anyone explain what's happening here?
c++ visual-c++ visual-studio-2012 stl
c++ visual-c++ visual-studio-2012 stl
edited Mar 12 at 9:01
Cerbrus
50k1096117
50k1096117
asked Jul 5 '13 at 8:25
Roger RowlandRoger Rowland
21.7k105688
21.7k105688
1
Most bizarre error message ever?
– curiousguy
Jul 5 '13 at 9:11
@NathanOliver: If you're editing posts to move error messages from quotes to code blocks, may I suggest adding<!-- language: lang-none -->to disable syntax highlighting?
– Cerbrus
Mar 12 at 9:01
add a comment |
1
Most bizarre error message ever?
– curiousguy
Jul 5 '13 at 9:11
@NathanOliver: If you're editing posts to move error messages from quotes to code blocks, may I suggest adding<!-- language: lang-none -->to disable syntax highlighting?
– Cerbrus
Mar 12 at 9:01
1
1
Most bizarre error message ever?
– curiousguy
Jul 5 '13 at 9:11
Most bizarre error message ever?
– curiousguy
Jul 5 '13 at 9:11
@NathanOliver: If you're editing posts to move error messages from quotes to code blocks, may I suggest adding
<!-- language: lang-none --> to disable syntax highlighting?– Cerbrus
Mar 12 at 9:01
@NathanOliver: If you're editing posts to move error messages from quotes to code blocks, may I suggest adding
<!-- language: lang-none --> to disable syntax highlighting?– Cerbrus
Mar 12 at 9:01
add a comment |
2 Answers
2
active
oldest
votes
std::map<T>::iterator is of the iterator-class bidirectional iterator. Those only have ++ and -- operators. +N and [] is only available for random access iterators (which can be found in e.g. std::vector<T>).
The reason behind this is that adding N to a random access iterator is constant time (e.g. add N*sizeof(T) to a T*), whereas doing the same thing for a bidirectional iterator would require applying ++ N times.
What you can do though (if you have C++11) is:
std::map<int, int>::const_iterator pIterTwo = std::next(pSomeMap.begin(),1);
which does the right thing for all iterator types.
2
That's excellent -std::nextlooks a lot cleaner to me too. Thanks +1.
– Roger Rowland
Jul 5 '13 at 8:34
@RogerRowland Note thatstd::nextis a C++11 addition. But if you don't have C++11, it is easy enough to implement your ownnextusingstd::advance. Or useboost::next.
– juanchopanza
Jul 5 '13 at 9:12
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
add a comment |
std::map iterators are bidirectional, thus they provide only ++ and -- operators, but not operator+, even if it is +1.
You can use std::advance if you really need to simulate operator+, but that would result into sequence of increment being called for the iterator.
1
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
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%2f17484354%2fwhy-cant-i-do-stdmap-begin-1%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
std::map<T>::iterator is of the iterator-class bidirectional iterator. Those only have ++ and -- operators. +N and [] is only available for random access iterators (which can be found in e.g. std::vector<T>).
The reason behind this is that adding N to a random access iterator is constant time (e.g. add N*sizeof(T) to a T*), whereas doing the same thing for a bidirectional iterator would require applying ++ N times.
What you can do though (if you have C++11) is:
std::map<int, int>::const_iterator pIterTwo = std::next(pSomeMap.begin(),1);
which does the right thing for all iterator types.
2
That's excellent -std::nextlooks a lot cleaner to me too. Thanks +1.
– Roger Rowland
Jul 5 '13 at 8:34
@RogerRowland Note thatstd::nextis a C++11 addition. But if you don't have C++11, it is easy enough to implement your ownnextusingstd::advance. Or useboost::next.
– juanchopanza
Jul 5 '13 at 9:12
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
add a comment |
std::map<T>::iterator is of the iterator-class bidirectional iterator. Those only have ++ and -- operators. +N and [] is only available for random access iterators (which can be found in e.g. std::vector<T>).
The reason behind this is that adding N to a random access iterator is constant time (e.g. add N*sizeof(T) to a T*), whereas doing the same thing for a bidirectional iterator would require applying ++ N times.
What you can do though (if you have C++11) is:
std::map<int, int>::const_iterator pIterTwo = std::next(pSomeMap.begin(),1);
which does the right thing for all iterator types.
2
That's excellent -std::nextlooks a lot cleaner to me too. Thanks +1.
– Roger Rowland
Jul 5 '13 at 8:34
@RogerRowland Note thatstd::nextis a C++11 addition. But if you don't have C++11, it is easy enough to implement your ownnextusingstd::advance. Or useboost::next.
– juanchopanza
Jul 5 '13 at 9:12
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
add a comment |
std::map<T>::iterator is of the iterator-class bidirectional iterator. Those only have ++ and -- operators. +N and [] is only available for random access iterators (which can be found in e.g. std::vector<T>).
The reason behind this is that adding N to a random access iterator is constant time (e.g. add N*sizeof(T) to a T*), whereas doing the same thing for a bidirectional iterator would require applying ++ N times.
What you can do though (if you have C++11) is:
std::map<int, int>::const_iterator pIterTwo = std::next(pSomeMap.begin(),1);
which does the right thing for all iterator types.
std::map<T>::iterator is of the iterator-class bidirectional iterator. Those only have ++ and -- operators. +N and [] is only available for random access iterators (which can be found in e.g. std::vector<T>).
The reason behind this is that adding N to a random access iterator is constant time (e.g. add N*sizeof(T) to a T*), whereas doing the same thing for a bidirectional iterator would require applying ++ N times.
What you can do though (if you have C++11) is:
std::map<int, int>::const_iterator pIterTwo = std::next(pSomeMap.begin(),1);
which does the right thing for all iterator types.
edited Apr 28 '16 at 15:24
Holt
25.8k65195
25.8k65195
answered Jul 5 '13 at 8:28
PlasmaHHPlasmaHH
13k33754
13k33754
2
That's excellent -std::nextlooks a lot cleaner to me too. Thanks +1.
– Roger Rowland
Jul 5 '13 at 8:34
@RogerRowland Note thatstd::nextis a C++11 addition. But if you don't have C++11, it is easy enough to implement your ownnextusingstd::advance. Or useboost::next.
– juanchopanza
Jul 5 '13 at 9:12
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
add a comment |
2
That's excellent -std::nextlooks a lot cleaner to me too. Thanks +1.
– Roger Rowland
Jul 5 '13 at 8:34
@RogerRowland Note thatstd::nextis a C++11 addition. But if you don't have C++11, it is easy enough to implement your ownnextusingstd::advance. Or useboost::next.
– juanchopanza
Jul 5 '13 at 9:12
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
2
2
That's excellent -
std::next looks a lot cleaner to me too. Thanks +1.– Roger Rowland
Jul 5 '13 at 8:34
That's excellent -
std::next looks a lot cleaner to me too. Thanks +1.– Roger Rowland
Jul 5 '13 at 8:34
@RogerRowland Note that
std::next is a C++11 addition. But if you don't have C++11, it is easy enough to implement your own next using std::advance. Or use boost::next.– juanchopanza
Jul 5 '13 at 9:12
@RogerRowland Note that
std::next is a C++11 addition. But if you don't have C++11, it is easy enough to implement your own next using std::advance. Or use boost::next.– juanchopanza
Jul 5 '13 at 9:12
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
@juanchopanza thanks, I have VS2012 which is probably the nearest that MS will ever get to C++11! It compiles and runs fine.
– Roger Rowland
Jul 5 '13 at 9:30
add a comment |
std::map iterators are bidirectional, thus they provide only ++ and -- operators, but not operator+, even if it is +1.
You can use std::advance if you really need to simulate operator+, but that would result into sequence of increment being called for the iterator.
1
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
add a comment |
std::map iterators are bidirectional, thus they provide only ++ and -- operators, but not operator+, even if it is +1.
You can use std::advance if you really need to simulate operator+, but that would result into sequence of increment being called for the iterator.
1
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
add a comment |
std::map iterators are bidirectional, thus they provide only ++ and -- operators, but not operator+, even if it is +1.
You can use std::advance if you really need to simulate operator+, but that would result into sequence of increment being called for the iterator.
std::map iterators are bidirectional, thus they provide only ++ and -- operators, but not operator+, even if it is +1.
You can use std::advance if you really need to simulate operator+, but that would result into sequence of increment being called for the iterator.
answered Jul 5 '13 at 8:28
alexrideralexrider
4,17611225
4,17611225
1
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
add a comment |
1
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
1
1
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
Why has it chosen to complain about string?
– doctorlove
Jul 5 '13 at 8:28
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
That's helpful, thanks, although like @doctorlove, I still don't understand the compiler error. I wonder what other compilers report.
– Roger Rowland
Jul 5 '13 at 8:32
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
@doctorlove Well it is better to ask msvc compiler developers. gcc gives all possible deductions. ideone.com/UUz5Xr
– alexrider
Jul 5 '13 at 8:35
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%2f17484354%2fwhy-cant-i-do-stdmap-begin-1%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
Most bizarre error message ever?
– curiousguy
Jul 5 '13 at 9:11
@NathanOliver: If you're editing posts to move error messages from quotes to code blocks, may I suggest adding
<!-- language: lang-none -->to disable syntax highlighting?– Cerbrus
Mar 12 at 9:01