如何将这些元素表达为 c++
How can I express this elements to c++
嗨,我想用 c++ 向量表达这个
int cnt;
for (int x = 0; x < sizeof(files) / sizeof(files[0]); x++) {
std::ifstream f;
f.open(files[x].c_str(), std::ios::in);
if (f.good()) {
while (!f.eof()) {//end of file check
f >> str;
bitset<8> s(str);
cnt++;
str.clear();
}
f.close();
}
for (int i = 0; i < cnt; i++){
vector<bitset<8>> s{ s[i] };
}
}
你的代码可以简化很多。下面是一个示例:
// Create the vector of bitsets. It is empty to start with.
vector<bitset<8>> s;
// Go through each file.
for (int x = 0; x < sizeof(files) / sizeof(files[0]); x++)
{
// Open the file.
// std::ifstream f(files[x].c_str()); // For pre C++11.
std::ifstream f(files[x]); // For C++11 or later.
// Define str here.
// It should not be needed outside the for loop.
std::string str;
// Keep reading from the file until read fails.
while (f >> str)
{
// Construct a bitset from the string.
bitset<8> si(str);
// Add the bitset to the vector of bitsets.
s.push_back(si);
}
// There is no need to explicitly close the file.
// The destructor will take care of that.
}
延伸阅读:为什么循环条件中的iostream::eof被认为是错误的?
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 从控制台中删除最后打印的元素
- 擦除while循环中迭代的元素