2

I am new to C++ and I was wondering how I can understand what functions and classes do. For example I was told to use "istringstream" for a homework assignment. I have looked online and found the site cplusplus.com with a lot of references. The problem I have is understanding the reference page.

On the "istringstream" reference page I am given the following code:

// istringstream constructors.
#include <iostream>     // std::cout
#include <sstream>      // std::istringstream
#include <string>       // std::string

int main () {

  std::string stringvalues = "125 320 512 750 333";
  std::istringstream iss (stringvalues);

  for (int n=0; n<5; n++)
  {
    int val;
    iss >> val;
    std::cout << val*2 << '\n';
  }

  return 0;
}

In the code above, it does exactly what I need it to do for my assignment but I do not understand WHY it works. So they created a istringstream object called iss, then later used "iss >> val". That is the part I am confused with. What exactly does it do?

I have tried reading the text above where it explains what each function in the class does but I did not understand any of it. For example, one of the first lines on the reference page says

default (1)   explicit istringstream (ios_base::openmode which = ios_base::in);

How do I interpret this line? From what I can see it is a function that takes one argument but what is "ios_base::openmode which = ios_base::in".

  • 1
    http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt -- If that leaves you confused, then you really need to cover some fundamentals and should read some introductory material on C++. – Iron Savior Sep 10 '14 at 15:34
  • You might start with re-reading any materials you have been given or if that does not help, find a basic tutorial or book on C++ to supplement that material. – crashmstr Sep 10 '14 at 15:35
  • Do you understand what `std::cin >> val;` would do? If so, it would be fairly easy to explain `istringstream`. If not, you need to gain more fundamental C++ understanding. – Fred Larson Sep 10 '14 at 15:40
  • If your homework is telling you to use iostreams before you're sure what a function is ... I guess you can just learn the magic incantation for now, and _how_ it works will be covered later. The documentation may be at the wrong level of abstraction for you right now. If your course _never_ explains how this works, it's a bad course and you should read a good book. – Useless Sep 10 '14 at 15:40
  • Thank you for the suggestions, do you guys have any good books on C++ for beginners that you recommend? If not I will try to look for some myself. – AdaptVPerish Sep 10 '14 at 18:05

3 Answers3

3

Think in std::istringstream as a stream interface adapter of std::string.

Is like writing that std::string in the console or file and read with std::cin or std::ifstream, it provide the interface (get(), peek(), etc...) for read the underlying storage (in this case std::string). The rest of the operator>> overloads probably use the basic reading functions to read the custom types (int, double, etc...).

The line: default (1) explicit istringstream (ios_base::openmode which = ios_base::in); is declaring a constructor that receive a default parameter name which with default value ios_base::in (this indicate that you could read from the stream)

NetVipeC
  • 4,402
  • 1
  • 17
  • 19
2

I believe this is the page you're looking for. This defines that mysterious >> operator. It is overloaded several different ways, which means it's working like a scanf function for several different data types.

Your compiler looks at iss >> val and says "Hmm...iss is an istringstream and val is an int. Aha! Looks like istringstream has an operator>> that takes an int&, I'll use that!"

Edit: istream has an operator>> for int&. istringstream inherits those.

Gutblender
  • 1,340
  • 1
  • 12
  • 25
1

later used "iss >> val". That is the part I am confused with. What exactly does it do?

The expression iss >> val is "syntactic sugar" for the function call iss.operator>>(val) where the formal parameter is defined as a reference to an int value. The function itself takes the address of val and writes a new value at this location. This is supposed to be "symbolized" by the >> symbol in your original expression iss >> val.

rubber boots
  • 14,924
  • 5
  • 33
  • 44