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

  • SEARCH
  • Home
  • 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 7778459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:24:09+00:00 2026-06-01T18:24:09+00:00

Here is some example code: #include <iostream> #include <vector> template <typename T> std::vector<typename T::iterator>

  • 0

Here is some example code:

#include <iostream>
#include <vector>

template <typename T>
std::vector<typename T::iterator> f(T t)
{
        std::vector<typename T::iterator> v;
        for (auto i = t.begin(); i != t.end(); ++i)
        {
                v.push_back(i);
        }
        return v;
}

template <typename T>
void print(const std::vector<T>& v)
{
        for (auto i = v.begin(); i != v.end(); ++i)
        {
                std::cout << **i << ' ';
        }
        std::cout << std::endl;
}

int main()
{
        std::vector<int> v{1, 2, 3};
        print(f(v));
        std::vector<std::vector<int>::iterator> itervec = f(v);
        print(itervec);
}

On ideone the output was:

1 2 3 
163487776 2 3 

Questions

If I change f(T t) to f(T& t) the output is as expected. I’m assuming because I am working with copies of containers, technically the iterators I am pushing back on the vector are not the same as the vector I created in main. Is this correct?
The one thing I noticed is print(f(v)); prints 1 2 3 as expected but as soon as I assign it to itervec the first iterator becomes garbage, is this all implementation dependent?

  • 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-01T18:24:10+00:00Added an answer on June 1, 2026 at 6:24 pm

    Yes, the iterators are iterators only valid for the local object v in the function f, and at the end of f, v goes out of scope and is destroyed, and the iterators are invalid.

    You have to pass the vector by reference (or pointer or whatever) so that the iterators you store are the iterators for the original object that the caller passes in, not for a temporary copy stored in a local variable.

    The behaviour you are seeing is undefined, so it just happens to print the first three and last two correctly.

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

Sidebar

Related Questions

Here is some code I wrote (using GCC's __restrict__ extension to C++): #include <iostream>
Here's some example code: class Obj attr :c, true def == that p '=='
I am developing a application using PHP. Some example code is here. $url =
For example (in my case) here's some code, <?php woo_post_inside_before(); if ( $woo_options['woo_post_content'] !=
I'm trying a basic input,output(and append) in C++ here is my code #include <iostream>
Here is some example data: data = data.frame(series = c(1a, 1b, 1e), reading =
I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
I'm working on a stored proc that executes some dynamic sql. Here's the example
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
I've been doing some reading into designing template code have a question about it.

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.