nsvir
nsvir

Reputation: 8951

Easy way to return an iterator content and increment it

I want to create a method which return a token and increment this one.

I did this:

_tokens is an std::istream_iterator

std::string             getToken()
{
  std::string           result;

  result = *_tokens;
  ++_tokens;
  return result;
}

As you can see this is kinda ugly cause I am returning a copy of a copy.

But I cannot return a reference on result cause result is a local variable.

And I cannot get the reference on *_tokens cause ++_tokens will change the content.

Have you got any idea how I could do this in a better way ?

Upvotes: 0

Views: 48

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

I think you mean the following

std::string             getToken()
{
  return *_tokens++;
}

Upvotes: 3

Related Questions