Like Tree4Likes
  • 1 Post By whiteflags
  • 3 Post By Elysia

Using a referenced vector in a fucntion

This is a discussion on Using a referenced vector in a fucntion within the C++ Programming forums, part of the General Programming Boards category; Can someone tell me what I am doing wrong here? Code: void print(const std::vector<int>& vec) { for ( std::vector<int>::iterator itr ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    56

    Using a referenced vector in a fucntion

    Can someone tell me what I am doing wrong here?

    Code:
    void print(const std::vector<int>& vec)
    {
        for ( std::vector<int>::iterator itr = vec.begin(), end = vec.end(); itr != end; ++itr )
        {
            std::cout << *itr << std::endl;
        }
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    Yes, your problem is that constant references to a vector object should be using constant things. There is something called const_iterator for vector, and any other STL container, in this situation.
    King Mir likes this.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Quote Originally Posted by whiteflags View Post
    Yes, your problem is that constant references to a vector object should be using constant things. There is something called const_iterator for vector, and any other STL container, in this situation.
    Excellent, thanks for your reply. I did not know there was a const_iterator. It works now that I am using that, thanks.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    If you are using C++11, then this...

    for ( std::vector<int>::iterator itr = vec.begin(), end = vec.end(); itr != end; ++itr )

    ...can become...

    for ( auto itr = vec.begin(), end = vec.end(); itr != end; ++itr )

    (Note how this code works whether or not your object is const or not1)

    ...or even...

    Code:
    for (const auto & item : vec)
    	std::cout << item << std::endl;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Thanks Elysia!

    I actually just read the article on C++11 today on this site, I am definitely interested in researching more into it as it seems to have enabled a lot of easier coding. For now, I am still a beginner trying to gather more experience in coding.

    Currently trucking my way through this sites exercises as I just finished reading the "Jumping into c++" book which was actually the first c++ book I've gone through that organized the learning process in a way that seems smart to me. Perhaps that is just the rookie in me talking, but it was a great read and easy to step through the learning the way it was organized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Erro: Referenced in function _main
    By skmightymouse in forum C Programming
    Replies: 4
    Last Post: 04-14-2012, 02:49 PM
  2. How C++ Objects are referenced
    By derder in forum C++ Programming
    Replies: 18
    Last Post: 08-01-2011, 10:22 AM
  3. Replies: 7
    Last Post: 02-01-2009, 03:53 PM
  4. What does de-referenced mean?
    By Gamma in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 11:06 AM
  5. How to change value of a referenced object.
    By sean in forum C++ Programming
    Replies: 4
    Last Post: 12-27-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21