Can we increase the iterator without the function? C++11
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I know we can use advance() function to increment the iterator. We also use iterator++ to increase the iterator by one position. Why we cannot use it+=2?
int main()
list<int> l11, 2, 3, 5, 6;
list<int> l22, 6, 8;
auto it = l1.begin();
advance(it, 2); //works
it++; //works
// it+=2; //not work
l2.splice(l2.begin(), l1, it);
for(int a: l2) cout<<a<<" ";
cout<<endl;
return 0;
You can run the above code here
c++ c++11 iterator listiterator
add a comment |Â
up vote
6
down vote
favorite
I know we can use advance() function to increment the iterator. We also use iterator++ to increase the iterator by one position. Why we cannot use it+=2?
int main()
list<int> l11, 2, 3, 5, 6;
list<int> l22, 6, 8;
auto it = l1.begin();
advance(it, 2); //works
it++; //works
// it+=2; //not work
l2.splice(l2.begin(), l1, it);
for(int a: l2) cout<<a<<" ";
cout<<endl;
return 0;
You can run the above code here
c++ c++11 iterator listiterator
4
List has a bidirectional iterator, not a random access iterator.advance
uses compile time machinery to optimize advance operation for different kinds of iterators.
â William Lee
24 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I know we can use advance() function to increment the iterator. We also use iterator++ to increase the iterator by one position. Why we cannot use it+=2?
int main()
list<int> l11, 2, 3, 5, 6;
list<int> l22, 6, 8;
auto it = l1.begin();
advance(it, 2); //works
it++; //works
// it+=2; //not work
l2.splice(l2.begin(), l1, it);
for(int a: l2) cout<<a<<" ";
cout<<endl;
return 0;
You can run the above code here
c++ c++11 iterator listiterator
I know we can use advance() function to increment the iterator. We also use iterator++ to increase the iterator by one position. Why we cannot use it+=2?
int main()
list<int> l11, 2, 3, 5, 6;
list<int> l22, 6, 8;
auto it = l1.begin();
advance(it, 2); //works
it++; //works
// it+=2; //not work
l2.splice(l2.begin(), l1, it);
for(int a: l2) cout<<a<<" ";
cout<<endl;
return 0;
You can run the above code here
c++ c++11 iterator listiterator
c++ c++11 iterator listiterator
asked 29 mins ago
Lusha Li
491
491
4
List has a bidirectional iterator, not a random access iterator.advance
uses compile time machinery to optimize advance operation for different kinds of iterators.
â William Lee
24 mins ago
add a comment |Â
4
List has a bidirectional iterator, not a random access iterator.advance
uses compile time machinery to optimize advance operation for different kinds of iterators.
â William Lee
24 mins ago
4
4
List has a bidirectional iterator, not a random access iterator.
advance
uses compile time machinery to optimize advance operation for different kinds of iterators.â William Lee
24 mins ago
List has a bidirectional iterator, not a random access iterator.
advance
uses compile time machinery to optimize advance operation for different kinds of iterators.â William Lee
24 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
8
down vote
operator +=
is only supported by RandomAccessIterator; note that it's supposed to have constant complexity.
The iterator of std::list
is BidirectionalIterator, which doesn't support operator +=
. (The iterator of std::vector
and std::array
is RandomAccessIterator.)
Note that both of them could be used with std::advance, when used for RandomAccessIterator complexity is constant; when used for other InputIterators (including BidirectionalIterator) complexity is linear. That means using std::advance
is a good idea because it's more general and could take advantage of the benefit of RandomAccessIterator automatically.
add a comment |Â
up vote
1
down vote
You cannot use += 2
with this iterator because in general case incrementing std::list<>
iterator by an arbitrary value is a relatively inefficient operation. +=
is not defined for your iterator type specifically to prevent you from carelessly/unknowingly using this inefficient operation in your code. Instead, if you really want to do that you are supposed to use std::advance
, which is a "red flag" function that is intended to highlight the fact that you are likely doing something inefficient. std::advance
is mostly intended for code sketching or for unlikely-to-get-executed fallback code. You are not supposed to use std::advance
in real production code. If you suddenly find yourself relying on std::advance
, it means that you probably need to redesign your data structures. Basically, std::advance
is like a cast - you are not supposed to use it unless you have a very very very good reason to.
Alternatively, you can use std::next
it = std::next(it, 2);
This function is much easier to use than std::advance
. By default this function is designed to advance the iterator by 1 step forward, but you can specify the second parameter to move it farther. Again, when the value of second parameter is potentially large, it should be considered a "red flag" function. A constant value of 2 is definitely within acceptable bounds.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
operator +=
is only supported by RandomAccessIterator; note that it's supposed to have constant complexity.
The iterator of std::list
is BidirectionalIterator, which doesn't support operator +=
. (The iterator of std::vector
and std::array
is RandomAccessIterator.)
Note that both of them could be used with std::advance, when used for RandomAccessIterator complexity is constant; when used for other InputIterators (including BidirectionalIterator) complexity is linear. That means using std::advance
is a good idea because it's more general and could take advantage of the benefit of RandomAccessIterator automatically.
add a comment |Â
up vote
8
down vote
operator +=
is only supported by RandomAccessIterator; note that it's supposed to have constant complexity.
The iterator of std::list
is BidirectionalIterator, which doesn't support operator +=
. (The iterator of std::vector
and std::array
is RandomAccessIterator.)
Note that both of them could be used with std::advance, when used for RandomAccessIterator complexity is constant; when used for other InputIterators (including BidirectionalIterator) complexity is linear. That means using std::advance
is a good idea because it's more general and could take advantage of the benefit of RandomAccessIterator automatically.
add a comment |Â
up vote
8
down vote
up vote
8
down vote
operator +=
is only supported by RandomAccessIterator; note that it's supposed to have constant complexity.
The iterator of std::list
is BidirectionalIterator, which doesn't support operator +=
. (The iterator of std::vector
and std::array
is RandomAccessIterator.)
Note that both of them could be used with std::advance, when used for RandomAccessIterator complexity is constant; when used for other InputIterators (including BidirectionalIterator) complexity is linear. That means using std::advance
is a good idea because it's more general and could take advantage of the benefit of RandomAccessIterator automatically.
operator +=
is only supported by RandomAccessIterator; note that it's supposed to have constant complexity.
The iterator of std::list
is BidirectionalIterator, which doesn't support operator +=
. (The iterator of std::vector
and std::array
is RandomAccessIterator.)
Note that both of them could be used with std::advance, when used for RandomAccessIterator complexity is constant; when used for other InputIterators (including BidirectionalIterator) complexity is linear. That means using std::advance
is a good idea because it's more general and could take advantage of the benefit of RandomAccessIterator automatically.
edited 11 mins ago
answered 22 mins ago
songyuanyao
86.7k10167227
86.7k10167227
add a comment |Â
add a comment |Â
up vote
1
down vote
You cannot use += 2
with this iterator because in general case incrementing std::list<>
iterator by an arbitrary value is a relatively inefficient operation. +=
is not defined for your iterator type specifically to prevent you from carelessly/unknowingly using this inefficient operation in your code. Instead, if you really want to do that you are supposed to use std::advance
, which is a "red flag" function that is intended to highlight the fact that you are likely doing something inefficient. std::advance
is mostly intended for code sketching or for unlikely-to-get-executed fallback code. You are not supposed to use std::advance
in real production code. If you suddenly find yourself relying on std::advance
, it means that you probably need to redesign your data structures. Basically, std::advance
is like a cast - you are not supposed to use it unless you have a very very very good reason to.
Alternatively, you can use std::next
it = std::next(it, 2);
This function is much easier to use than std::advance
. By default this function is designed to advance the iterator by 1 step forward, but you can specify the second parameter to move it farther. Again, when the value of second parameter is potentially large, it should be considered a "red flag" function. A constant value of 2 is definitely within acceptable bounds.
add a comment |Â
up vote
1
down vote
You cannot use += 2
with this iterator because in general case incrementing std::list<>
iterator by an arbitrary value is a relatively inefficient operation. +=
is not defined for your iterator type specifically to prevent you from carelessly/unknowingly using this inefficient operation in your code. Instead, if you really want to do that you are supposed to use std::advance
, which is a "red flag" function that is intended to highlight the fact that you are likely doing something inefficient. std::advance
is mostly intended for code sketching or for unlikely-to-get-executed fallback code. You are not supposed to use std::advance
in real production code. If you suddenly find yourself relying on std::advance
, it means that you probably need to redesign your data structures. Basically, std::advance
is like a cast - you are not supposed to use it unless you have a very very very good reason to.
Alternatively, you can use std::next
it = std::next(it, 2);
This function is much easier to use than std::advance
. By default this function is designed to advance the iterator by 1 step forward, but you can specify the second parameter to move it farther. Again, when the value of second parameter is potentially large, it should be considered a "red flag" function. A constant value of 2 is definitely within acceptable bounds.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You cannot use += 2
with this iterator because in general case incrementing std::list<>
iterator by an arbitrary value is a relatively inefficient operation. +=
is not defined for your iterator type specifically to prevent you from carelessly/unknowingly using this inefficient operation in your code. Instead, if you really want to do that you are supposed to use std::advance
, which is a "red flag" function that is intended to highlight the fact that you are likely doing something inefficient. std::advance
is mostly intended for code sketching or for unlikely-to-get-executed fallback code. You are not supposed to use std::advance
in real production code. If you suddenly find yourself relying on std::advance
, it means that you probably need to redesign your data structures. Basically, std::advance
is like a cast - you are not supposed to use it unless you have a very very very good reason to.
Alternatively, you can use std::next
it = std::next(it, 2);
This function is much easier to use than std::advance
. By default this function is designed to advance the iterator by 1 step forward, but you can specify the second parameter to move it farther. Again, when the value of second parameter is potentially large, it should be considered a "red flag" function. A constant value of 2 is definitely within acceptable bounds.
You cannot use += 2
with this iterator because in general case incrementing std::list<>
iterator by an arbitrary value is a relatively inefficient operation. +=
is not defined for your iterator type specifically to prevent you from carelessly/unknowingly using this inefficient operation in your code. Instead, if you really want to do that you are supposed to use std::advance
, which is a "red flag" function that is intended to highlight the fact that you are likely doing something inefficient. std::advance
is mostly intended for code sketching or for unlikely-to-get-executed fallback code. You are not supposed to use std::advance
in real production code. If you suddenly find yourself relying on std::advance
, it means that you probably need to redesign your data structures. Basically, std::advance
is like a cast - you are not supposed to use it unless you have a very very very good reason to.
Alternatively, you can use std::next
it = std::next(it, 2);
This function is much easier to use than std::advance
. By default this function is designed to advance the iterator by 1 step forward, but you can specify the second parameter to move it farther. Again, when the value of second parameter is potentially large, it should be considered a "red flag" function. A constant value of 2 is definitely within acceptable bounds.
edited 2 mins ago
answered 7 mins ago
AnT
254k31404651
254k31404651
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53002491%2fcan-we-increase-the-iterator-without-the-function-c11%23new-answer', 'question_page');
);
Post as a guest
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
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
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
4
List has a bidirectional iterator, not a random access iterator.
advance
uses compile time machinery to optimize advance operation for different kinds of iterators.â William Lee
24 mins ago