Questions tagged [extraction-operator]

Anything related to the C++ extraction operator for standard streams (`>>`), i.e. the operator used to perform input operations from streams. Despite being syntactically identical, it is not related to the right-shift operator or C++.

Anything related to the C++ extraction operator for standard streams (>>), i.e. the operator used to perform input operations from streams. Despite being syntactically identical, it is not related to the right-shift operator or C++.

28 questions
9
votes
1 answer

How are iomanip functions Implemented?

Some of the standard iomanip functions take take a parameter. I'd like to know how this is accomplished, for instance, can I do something similar with a function? That's really the solution that I needed for this answer, but I couldn't figure out…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
7
votes
2 answers

How do you use the extraction operator (>>) with vector?

In an example with vector someVector and istringstream someStringStream you can do this: for (int i=0; i < someVector.size(); i++) { someStringStream >> someVector[i]; } I know that vector is an efficient implementation, and operator[]…
6
votes
2 answers

Forcing String to int Function to Consume Entire String

Given a string that should represent a number, I'd like to put it into a conversion function which would provide notification if the whole string did not convert. For input: "12": istringstream::operator>> outputs 12 atoi outputs 12 stoi outputs…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
2 answers

Is it OK to continue reading from stream after failure?

I am using the following loop to read numbers from two files until both are exhausted: int a, b; while (file1 >> a, file2 >> b, file1 || file2) { if (file1 && file2) ... // use of both a and b if (file1) ... // use of a if (file2) ... //…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
3
votes
2 answers

istream_iterator Does Not Zero-Initialize

This is an Minimal, Complete, Verifiable Example I understand that this is not copacetic. Anyway, given the struct: struct Foo { int even; int odd; }; istream& operator>>(istream& lhs, Foo& rhs) { int input; lhs >> input; …
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
1 answer

Unexpected Line Breaks in Output

In this answer I have the following code: #include #include #include #include #include #include using namespace std; struct station{ string _stationName; int _studentPass; int…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
0 answers

Extraction operator causing my program to exit?

I'm a usual lurker but this is my first post! I understand you guys like detail so I will do my best. I will appreciate whatever input anyone has. I am working on an overloading the extraction operator for an object with a dynamic array of digits.…
spanishgum
  • 1,030
  • 1
  • 14
  • 29
2
votes
1 answer

Why Can't I do decltype on an Extraction Operator

It seems like this should be legal: decltype(declval().operator>>(declval(), declval())) test; But when I try to compile I get: error C2661: std::basic_istream>::operator >>: no overloaded…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
3 answers

Why are non-const vector elements const?

When I iterate over a vector, I find that the elements, dereferenced through the iterator, are recognized as if they were const. Why is that? Change either the container or the element type, such as list or vector, and the…
plong
  • 1,723
  • 3
  • 14
  • 14
2
votes
3 answers

How can I read from an std::istream (using operator>>)?

How can I read from an std::istream using operator>>? I tried the following: void foo(const std::istream& in) { std::string tmp; while(in >> tmp) { std::cout << tmp; } } But it gives an error: error: no match for 'operator>>' in 'in >>…
Frank
  • 64,140
  • 93
  • 237
  • 324
2
votes
2 answers

stringstream extraction not working

I seem to be having a problem with extracting data from a stringstream. The start of my extraction seems to be missing the first two characters. I have something similar to the following code: std::stringstream ss( std::stringstream::in | …
Seth
  • 8,213
  • 14
  • 71
  • 103
1
vote
0 answers

Best way to Ignore an Input Character in a Stream

Given the input: const char input[] = "lorem\t, ipsum" and the expected outputs: char first[size(input)] and second[size(input)] I can do this: sscanf(input, "%s , %s", first, second); Ignoring white-space and the delimiting character with ,. I'd…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
0 answers

Extraction operator overloading to read from a file stream with multiple data types

I have a text file containing several book names and their correlating ISBN numbers in the format shown below, names separated by a tab space. Neuromancer 345404475 DUNE 441172717 Ringworld 345020464 The Expanse 441569595 Do Androids…
Woodman
  • 11
  • 2
1
vote
2 answers

Is it guaranteed that standard extraction operator>> does not change argument in case of failure?

If calling something like input_stream >> i; where i is of arithmetic type, throws exception or sets badbit etc., is it guaranteed that i has not changed?
Dmitry J
  • 867
  • 7
  • 20
1
vote
2 answers

Implementing the extraction and insertion operator C++

I have a basic Student class (has to be a class)(yes there is no encapsulation and also namespace pollution pls forgive) and I would like to create custom extraction and insertion operators. After countless searches it still would not work. What I…
Boris N
  • 33
  • 5
1
2