我正在阅读使用istream_iterators构造一个向量,这个向量是将一个完整的文件内容读入一个字符向量中.虽然我想将文件的一部分加载到字符向量中.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
ifstream ifs(argv[1], ios::binary);
istreambuf_iterator<char> beginItr(ifs);
istreambuf_iterator<char> endItr(beginItr);
advance(endItr, 4);
vector<char> data(beginItr, endItr);
for_each(data.cbegin(), data.cend(), [](char ch)
{
cout << ch << endl;
});
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为提前不起作用,而上述问题的接受答案有效.为什么不提前工作istreambuf_iterator?
也
endItr++;
endItr++;
endItr++;
endItr++;
cout << distance(beginItr, endItr) << endl;
Run Code Online (Sandbox Code Playgroud)
返回0.请有人解释发生了什么!
在尝试编译以下内容时:
#include <string>
#include <iterator>
#include <iostream>
using namespace std;
int main() {
string s(istream_iterator<char>(cin), istream_iterator<char>());
return s.size();
}
Run Code Online (Sandbox Code Playgroud)
g ++ 4.4.1给了我:
main.cc: In function ‘int main()’:
main.cc:6: error: request for member ‘size’ in ‘s’, which is of non-class type ‘std::string(std::istream_iterator<char, char, std::char_traits<char>, int>, std::istream_iterator<char, char, std::char_traits<char>, int> (*)())’
Run Code Online (Sandbox Code Playgroud)
根据libstdc ++ docs,string有一个ctor,它接受一个开始/结束迭代器对.为什么我会收到此错误?
如果您正在从文件中逐行输入(将行读入字符串,用于标记化),是否有理由提前使用getline或istream_iterator.
我有一个表示字符序列的类,我想operator >>为它实现一个.我的实现目前看起来像这样:
inline std::istream& operator >>(std::istream& in, seq& rhs) {
std::copy(
std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::back_inserter(rhs));
// `copy` doesn't know when to stop reading so it always also sets `fail`
// along with `eof`, even if reading succeeded. On the other hand, when
// reading actually failed, `eof` is not going to be set.
if (in.fail() and in.eof())
in.clear(std::ios_base::eofbit);
return in;
}
Run Code Online (Sandbox Code Playgroud)
但是,以下可预测的失败:
std::istringstream istr("GATTACA FOO");
seq s;
assert((istr >> s) and s == "GATTACA");
Run Code Online (Sandbox Code Playgroud)
特别是,一旦我们到达" GATTACA FOO"中的空格,复制停止(预期)并将故障位置设置为istream …
我有一个以下容器,我想存储在一个文件中:
std::vector< std::vector< Point > > m_vPoints;
Run Code Online (Sandbox Code Playgroud)
Point是一个定义operator<<()和operator>>().的基本结构.
该文件的格式为:
point0_0 point0_1 point0_2 ... <-- points from m_vPoints[0]
point1_0 point1_1 ... <-- points from m_vPoints[1]
...
Run Code Online (Sandbox Code Playgroud)
点元素由分隔',',并且点由' '例如:
-5,4 6,12 -7,32 ...
12,0 -3,4 ...
Run Code Online (Sandbox Code Playgroud)
我设法生成这样一个文件:
std::ostream& operator<<(std::ostream &o, ...)
{
for(auto it=m_vPoints.begin(); it!=m_vPoints.end(); ++it)
{
copy(it->begin(), it->end(), std::ostream_iterator<Point>(o," "));
o << endl;
}
return o;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.然而问题是阅读.当我尝试以下内容时:
std::istream& operator>>(std::istream &is, ...)
{
int numberOfRows; // assume it is known and valid …Run Code Online (Sandbox Code Playgroud) 给定包含以下十六进制代码的文件:0B 00 00 00 00 00 20 41
我正在尝试填充std :: vector <std :: uint8_t>,然后手动检查每个字节.
这是我使用迭代器构造函数从两个std :: istream_iterators创建向量的代码
using Bytes = std::vector<std::uint8_t>;
using ByteItr = std::istream_iterator<std::uint8_t>;
Bytes getBytes()
{
std::ifstream in;
in.open("filepath");
in.seekg(0, std::ios::beg);
Bytes bytes;
ByteItr start(in);
ByteItr end;
return Bytes(start, end);
}
Run Code Online (Sandbox Code Playgroud)
这是我试图通过的单元测试:
auto bytes = getBytes();
REQUIRE( bytes.size() == 8 );
CHECK( bytes[0] == 0x0B );
CHECK( bytes[1] == 0x00 );
CHECK( bytes[2] == 0x00 );
CHECK( bytes[3] == 0x00 );
CHECK( bytes[4] == 0x00 ); …Run Code Online (Sandbox Code Playgroud) 一种读取文件并将其作为字节数组放入向量的方法是:
std::ifstream input(filePath, std::ios::binary);
std::vector<unsigned char> barray(std::istreambuf_iterator<char>(input), {});
Run Code Online (Sandbox Code Playgroud)
据我了解,std::vector以上代码片段中使用的构造函数是
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)
因此,{}对应于last。
到底是什么{}?它像空/空迭代器一样工作吗?
我有一个遗留代码,其中为指针定义了接口.我试图调整一些函数来获取迭代器,例如转发迭代器.
是否允许使用InputIterator解除引用的元素的地址,例如istream_iterator?
结果是暂时的,并且必须在内存的某个地方进行通话,但我不确定.
以下示例使用double,但类型可能更复杂(大).
#include<iostream>
#include<iterator>
#include<sstream>
void f_legacy(double const* t){
std::cout << *t << std::endl;
};
void f(std::istream_iterator<double> it){
f_legacy(std::addressof(*it)); // OK????
// to avoid a copy: auto v = *it; f_legacy(std::addressof(v));
}
int main(){
double d = 5.;
std::istringstream iss("1 2 3");
std::istream_iterator<double> it(iss);
f_legacy(&d);
f(it);
}
Run Code Online (Sandbox Code Playgroud) vector<int> data(istream_iterator<int>(cin),
istream_iterator<int>{}); cout<<"Size is : " << data.size() << endl; //compile success
vector<int> data1(istream_iterator<int>(cin),
std::allocator<int>{}); cout<<"Size is : " << data1.size() << endl; //compile failure
Run Code Online (Sandbox Code Playgroud)
error: no matching function for call to ‘std::vector<int>::vector(std::istream_iterator<int>, std::allocator<int>)’ vector<int> data1(istream_iterator<int>(cin), std::allocator<int>{});
Run Code Online (Sandbox Code Playgroud)
为什么第一个语句很好,但第二个呢?int在这种情况下,向量不采用类型分配器吗?我正在试验allocators。