Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8910877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:48:17+00:00 2026-06-15T03:48:17+00:00

For example, std::vector<int>::iterator it = –(myVec.end()); . This works in GCC 4.4 but I

  • 0

For example, std::vector<int>::iterator it = --(myVec.end());. This works in GCC 4.4 but I have heard a rumor that it’s not portable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:48:18+00:00Added an answer on June 15, 2026 at 3:48 am

    It will only work if std::vector<int>::iterator is an object type with operator++ a member function. If it’s a scalar type (e.g. int *), or operator++ is a non-member function, it will fail.

    5.3.2 Increment and decrement [expr.pre.incr]

    1 – The operand of prefix ++ is modified by adding 1 […]. The operand shall be a modifiable lvalue. […]
    2 – […] The requirements on the operand of prefix -- […] are […] the same as those of prefix ++. […]

    Non-const non-static member functions can be called on temporary objects (since they have non-const object type, per 9.3.2p3), but an lvalue reference parameter in a non-member function cannot bind to a temporary (13.3.3.1.4p3).

    struct S { S &operator++(); };
    struct T { }; T &operator++(T &);
    typedef int U;
    
    ++S();  // OK
    ++T();  // fails
    ++U();  // fails
    

    This means that it’s nothing to do with the compiler, but rather the standard library; as you’ve observed libstdc++ is implemented with std::vector<int>::iterator an object type with member operator++, but your code could easily be compiled with the same compiler and a different standard library where std::vector<int>::iterator is int *, in which case it would fail.

    std::vector, std::array and std::string are the only container templates that can sensibly be implemented with scalar (pointer) iterators, but that doesn’t mean that calling ++ on other containers’ iterators is safe; they could have non-member operator++ as T above.

    To make an iterator to the before-the-end element, use std::prev:

    std::vector<int>::iterator it = std::prev(myVec.end());
    

    std::prev and std::next are new in C++11, but are easily implementable in C++03.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() {
Is this example code valid? #include<vector> using namespace std; int main() { vector<int> vec(10);
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
For example is the following valid? std::vector<int> vec(5, 0); std::vector<int>::const_iterator it1(vec.begin()); std::vector<int>::const_iterator it2(vec.begin()); //Use
I have a sorted std::vector<unsigned long long int> myvector (and all values are different).
Consider the following minimal example. #include <vector> class Data { std::vector<int>& v; public: Data(std::vector<int>&
Here is some example code: #include <iostream> #include <vector> template <typename T> std::vector<typename T::iterator>
What is the cheapest way to initialize a std::vector from a C-style array? Example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.