131 questions
2
votes
2
answers
123
views
Why can I not efficiently move the strings when using std::istream_iterator<std::string>?
#include <fstream>
#include <string>
#include <vector>
int main() {
auto fin = std::ifstream("tmp.txt");
auto pos = std::istream_iterator<std::string>(fin);...
2
votes
1
answer
60
views
std::istreambuf_iterator equality when using seekg
I am puzzled by the output of the code below:
std::string content = "abcdefghijklmnopqrstuvwxyz1234567890";
std::istringstream iss(content);
iss.seekg(10);
std::istreambuf_iterator<char&...
4
votes
1
answer
289
views
Why can't I use lazy_split_view with a istream view instead of a string_view?
Consider this code snippet (adapted from cppreference.com):
constexpr std::string_view text{"Hello-+-C++-+-23-+-!"};
constexpr std::string_view delim{"-+-"};
std::ranges::for_each(...
0
votes
1
answer
95
views
Is `std::istream_iterator<float>` reading out-of-range number undefined behavior?
const std::string str = "-5.73945e-39";
std::istringstream iss(str);
auto vals = std::vector<float>(std::istream_iterator<float>(iss), std::istream_iterator<float>{});
If ...
-1
votes
2
answers
534
views
How to read from a binary file and load the data into a 3d vector in C++?
I currently have some .bin files, each of which contains a matrix of size AxBxC. I would like to load it into a 3d vector vector<vector<vector<float>>> in C++, but encountered some ...
2
votes
1
answer
281
views
Why doesn't std::istream_iterator< std::string_view > compile?
Why can't GCC and Clang compile the code snippet below (link)? I want to return a vector of std::string_views but apparently there is no way of extracting string_views from the stringstream.
#include &...
1
vote
1
answer
316
views
Read bitset from file using istream_iterator
I'm trying to read a text file into a vector of std::bitset-objects. The file consists of 1's and spaces (for indicating 0 or false) and I'm trying to do this by overloading the input operator>> ...
1
vote
1
answer
99
views
Initializing a vector with istream_iterator before while loop, does this operation affect following loop?
When adding vector<string> vec(file_iter, eof); before the while loop, that loop will run only once. Why?
istream_iterator<string> file_iter(in), eof;
map<string, size_t> word_cout;
...
1
vote
0
answers
31
views
C++ istream_iterator doesn't work after initial use [duplicate]
I'm learning about C++ iterators and I'm having trouble with the following piece of code. It simply reads in a text file data.txt that has a series of integers in it, and prints the contents to the ...
0
votes
2
answers
433
views
istream_iterator behavior misunderstanding
The goal is to read 16 bit signed integers from a binary file. First, I open the file as an ifstream, then I would like to copy each numbers into a vector using istream_iterator and the copy algorithm....
-5
votes
1
answer
400
views
Using iterator to read input [closed]
I have the following code, which has sin_start(cin) function. I am not sure if there is any such function defined in C++ or if I need to define it myself. But somehow this code compiles and runs too.
#...
0
votes
1
answer
283
views
What happens when std::istream_iterator<int>(std::cin) Equals to the end iterator std::istream_iterator<int>()
i am learning about iterators by checking/writing different examples. In one such example(given below) when i enter an invalid type say char into the input stream the next cout statement is not ...
0
votes
2
answers
399
views
c++ : istream_iterator skip spaces but not newline
Suppose I have
istringstream input("x = 42\n"s);
I'd like to iterate over this stream using std::istream_iterator<std::string>
int main() {
std::istringstream input("x = 42\n&...
1
vote
1
answer
3k
views
Apache Camel with SQL component for inserts with batch=true throws error with multiple tables
I am using Apache camel with sql component for performing sql operations with Postgresql. I have already tried successfully inserting multiple rows in a single table as a batch using batch=true option ...
0
votes
0
answers
249
views
copy_if() stream_iterator works with C++20 but not C++17
$ cat so.cpp
#include <iostream>
#include <string>
#include <iterator>
int main()
{
std::istream_iterator<char> cin_it(std::cin),eos;
std::ostream_iterator&...
-1
votes
1
answer
311
views
What's with allocator in vector?
vector<int> data(istream_iterator<int>(cin),
istream_iterator<int>{}); cout<<"Size is : " << data.size() << endl; //compile success
vector<int> ...
4
votes
2
answers
210
views
Why is reading from an istream_iterator assigned to a variable not working?
I wrote the following program that reads in 3 numbers from std::cin, and outputs them to std::cout, and does this twice:
#include <iostream>
#include <algorithm>
#include <iterator>
...
3
votes
2
answers
3k
views
I can't understand the use of std::istream_iterator
I can't understand the code below.
(from https://www.boost.org/doc/libs/1_74_0/more/getting_started/unix-variants.html)
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <...
4
votes
1
answer
277
views
Why std::istream_iterator<> with multiple copy_n() always writes firs value
I tried to copy the input line into multiple vectors:
#include <vector>
#include <sstream>
#include <istream>
#include <iterator>
#include <algorithm>
#include <...
1
vote
2
answers
1k
views
C++ count the number of words from standard input
I saw a piece of C++ code to count the number of words inputted from standard input:
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main() {
...
0
votes
1
answer
348
views
Why does appear "passing 'const Person' as 'this' argument discards qualifiers" while using STL copy algorithm?
I'm trying to implement a max heap on user defined structures, and I use the std::copy_n() algorithm to take input from heap.in file, but I receive this kind of error from the compiler:
passing '...
0
votes
1
answer
349
views
Is there a istream const iterator?
Is there such a thing as a std::istream const iterator?
The following code won't compile because the std::istream_iterator in foo() can't bind to the const std::istream reference to the temporary ...
3
votes
1
answer
49
views
Why do successive istream_iter objects increment the iterated stream?
Why does the following code output c?
// main.cpp
#include <iostream>
#include <sstream>
#include <iterator>
int main( int argc, char* argv[] )
{
std::string p( "...
0
votes
1
answer
283
views
Extracting complete current line from std::istream
I am scanning text coming from a std::istream. The scanning is already under way, and I would like to extract the line that is currently being read (from beginning to end). This getCurrentLine() ...
0
votes
1
answer
270
views
Using stream to treat received data
I am receiving messages from a socket.
The socket is packed within a header (that is basically the size of the message) and a footer that is a crc (a kind of code to check if the message is not ...
1
vote
3
answers
479
views
Why am I getting seg faults from using the istream iterator?
void parse_and_run_command(const std::string &command) {
std::istringstream iss(command);
std::istream_iterator<char*> begin(iss), end;
std::vector<char*> tokens(begin, end)...
1
vote
1
answer
179
views
Use of '{}' as end iterator in std::vector constructor
A way for reading a file and put it as a byte array into a vector would be:
std::ifstream input(filePath, std::ios::binary);
std::vector<unsigned char> barray(std::istreambuf_iterator<char&...
6
votes
1
answer
948
views
Can I use istream_iterator<char> to copy some istream content into std::string?
I have an istream and need to copy the content between two delimiters to a std::string.
I can find the delimiters' streampos, but when trying to use istream_iterator<char> to iterate over the ...
0
votes
0
answers
56
views
How Can I initialize a vector of integers directly from a range of elements denoted by input-stream-iterators? [duplicate]
Again reading C++ Primer 5th Edition: I am practicing stream iterators. Here is an example I can't really understand:
In the book there's an example like this:
std::istream_iterator<int> ...
0
votes
1
answer
61
views
C++ Primer 5 Edition: Stream iterators and Sales_item
In C++ primer 5Ed the chapter talks about stream iterators he gives this example (page 512):
int main(){
istream_iterator<Sales_item> item_iter(cin), eof;
ostream_iterator<...
3
votes
2
answers
109
views
Why I cannot create a stream iterator?
I am supposed to create a looks-like stream iterator class so that I can read from the input stream when incrmenting an object of my class.
I've done this:
template<class T>
struct istrm_it{
...
1
vote
2
answers
2k
views
How to read strings with spaces from a ifstream using istream_iterator?
I want to read a text file, line by line, using istream_iterator, but it fails when there is a white space in the line.
This is a sample code:
#include <fstream>
#include <iomanip>
#...
4
votes
4
answers
2k
views
How to make std::istream_iterator read only until the end of line?
Having the following piece of code:
std::vector<int64> values;
std::copy(
std::istream_iterator<int64>(std::cin),
std::istream_iterator<int64>(),
std::back_inserter(values)
);
...
0
votes
1
answer
42
views
understanding equality from istream_iterator
Can someone please explain why this istream_iterator code.
// code extract from SGI STL istream_iterator class
bool _M_equal(const istream_iterator& __x) const {
return (_M_ok == __x.M_ok) &...
0
votes
1
answer
715
views
I want to use cin after read input with istream operator
I use istream_iterator to read integers from input (until eof) and store them to a vector
after that i want to read a single integer (or maybe a value of another type e.g. string). How can i do that?
...
1
vote
0
answers
65
views
Encapsulate data access using own istream_iterator
I currently have code that essentially contains these commands:
std::string line;
std::getline(ifs, line);
ifs is some std::ifstream. The code works.
I now would like to use the same on data I ...
14
votes
5
answers
826
views
Multi stream iterators c++
The purpose of my program is to open a text file of m lines of the same length n, read the file column by column and print each column.
For example, for this text file
abcd
efgh
jklm
I would like ...
3
votes
0
answers
39
views
std::istream_iterator doesn't work with std::pair [duplicate]
Simple example. This code doesn't compile:
#include <iostream>
#include <iterator>
using test_t = std::pair<int, int>;
std::istream& operator>>(std::istream& in, ...
1
vote
1
answer
92
views
c++ fstream obj rollback after using in istream_iterator
How to reset iterator state or use fstream obj, which was in use by istream_iterator, in other istream_iterator. I tried seekp(ios_base::begin) and clear() methods on fstream obj but this doesn't do ...
1
vote
1
answer
188
views
C++ istreambuf_iterator template parameter
Based on this question: C++ streams confusion: istreambuf_iterator vs istream_iterator? on istreambuf_iterator, my understanding is that istreambuf_iterator is an iterator for raw input rather than ...
1
vote
1
answer
824
views
std::transform with std::istream_iterator
Say I have a string containing numbers separated by empty spaces. I want to insert the numbers into a vector of integers, where every number is decreased by one. So for example:
"1 2 3 4" -> {0, 1,...
1
vote
6
answers
204
views
Unable to understand this C++ program return value
I came across the following program in TCPPPL by Stroustrup:
int main()
{
string from, to;
cin >> from >> to; // get source and target file names
ifstream is {from}; // ...
1
vote
2
answers
554
views
istream_iterator copy example keeps waiting for input
I tried implementing an example of stream iterators from page 107 of "The C++ Standard Library". I get stuck on this line:
copy (istream_iterator<string>(cin), istream_iterator<string>(), ...
0
votes
1
answer
283
views
How to retrieve file size from range-v3's istream_range?
I'm trying to get file size value from range-v3, like this.
std::ifstream i("test.bin", std::ios::binary | std::ios::in);
auto rng = ranges::istream_range<unsigned char>(i);
std::cout << ...
1
vote
0
answers
81
views
Optimize InputIterator dereference without making a copy if possible?
I have a legacy code in which the interface is defined for pointer only and I am trying to adapt some functions to take iterators.
In the answers to this question Address of a dereferenced ...
0
votes
1
answer
164
views
Address of a dereferenced InputIterator? The case of istream_iterator
I have a legacy code in which the interface is defined for pointer.
I am trying to adapt some functions to take iterators, e.g. forward iterators.
Is one allowed to take the address of the element ...
0
votes
0
answers
26
views
Read csv file into vector using istream_iterator in C++ [duplicate]
I'm trying to write a simple function that reads a csv while and stores each line into a vector. The code is given below:
vector<string> readCSVfile( string path )
{
ifstream ...
0
votes
1
answer
210
views
Does istream_iterator reuse object?
I'm writing a C++ library to parse WARC files, it contains the classes to store the WARC record and fields data as well as the operator to read the data from some input stream. It has the operator>&...
0
votes
2
answers
2k
views
c++ how to read a file and split it's lines
I'm trying to read a file and split each line (so instead of the line "I walk my dog every day" I would have "I", "walk", "my", "dog", "every", "day"). I tried to follow the example from here.
This ...
-2
votes
2
answers
145
views
istream_iterator behaviour on 0x9-0xD
I have written a small test file to make the question clear :
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cstdio>
#include <sstream>
void ...