为什么vector总是空的?
Why does vector always stay empty?
因此,程序将文件夹的路径作为命令行选项,然后读取文件夹中的所有文件,如果文件内容正确(应该是整数),则输出文件名和整数(例如:test.txt
: 192
), check()
函数确定文件数据是否正确。
在所有好的文件都打印出来之后,我也想打印所有的坏文件。所以我试着在vector<string>
中收集他们的名字,但不幸的是它总是空的。所有的逻辑都在print()
函数中,因为程序应该使用多线程处理文件。当我不使用线程时,一切都很好。
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<iomanip>
#include<regex>
#include<boost/algorithm/algorithm.hpp>
#include<boost/range/algorithm.hpp>
#include<boost/filesystem.hpp>
#include<locale>
#include<thread>
using std::cout;
using std::endl;
using namespace boost::filesystem;
using vec = std::vector<std::string>;
static int summ = 0;
static vec vc;
inline auto check(const std::string &s)
{
std::regex reg1("[[:blank:]]*[-[:digit:]]*[[:blank:]]*", std::regex_constants::ECMAScript);
return regex_match(s, reg1);
}
void print(boost::filesystem::directory_entry &dir,std::vector<std::string> &v)
{
std::ifstream is;
is.open(dir.path().string());
std::stringstream buf;
buf << is.rdbuf();
auto temp = buf.str();
if(!check(temp))
{
vt.push_back(dir.path().filename().string());
is.clear();
is.close();
return;
}
cout.setf(std::ios::left, std::ios::adjustfield);
cout << std::setw(20) << dir.path().filename().string() << ": " << std::stoi(temp) << endl;
is.clear();
is.close();
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
int sum{};
std::vector<std::string> vt;
std::ifstream file;
if (argc < 2)
{
cout << "usage: prog path" << endl;
return 1;
}
boost::filesystem::path p(argv[1]);
if (!boost::filesystem::is_directory(p))
{
cout << "not a directory!";
return 1;
}
for (auto &el : boost::filesystem::directory_iterator(p))
{
if (!boost::filesystem::is_directory(el))
{
std::thread tr(print, el,vc);
tr.join();
}
}
//cout << "Sum: " << sum << endl;
cout << "Files with wrong input data: n";
boost::copy(vt, std::ostream_iterator<std::string>(cout, "n")); //empty here
}
问题:
vt
为空,因为您从未向其添加任何内容。你把vc
传递给print
。即使您将vt
传递给print
,它也不会编译,因为您在print
中对不存在的变量vt
调用push_back
。您可能指的是v
,即参数的名称。
解决方案:
- 去掉
main
中的 - 在
print
中调用v.push_back
而不是vt.push_back
static vec vc;
或std::vector<std::string> vt;
:
- std::向量。Push_back不是线程安全的,所以如果你同时在多个线程上访问它,你应该锁定它。你没有,因为: 你实际上没有并行任何东西,因为你在启动每个线程后立即加入。
- 磁盘IO将成为瓶颈,所以即使你并行检查,也可能不会更快。
相关文章:
- 为什么(-1)%vector::size()总是返回0
- 为什么std::vector比数组慢
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么std::vector和std::valarray初始化构造函数不同
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 为什么当 vector 为空时会显示运行时错误?
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么初始化 std::vector 时不能使用模板整数?
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 为什么 std::vector 需要 is_trivial 才能按位移动,而不仅仅是is_trivially_copya
- 为什么 std::vector 使用 std::分配器而不是运算符 new 和 delete?
- 为什么使用 std::vector 的代码不能编译,但使用 std::unique_ptr 如果没有 noexcept
- unqualified sort() -- 为什么它在 std::vector 上使用而不是在 std::array 上
- 为什么vector::eras似乎会导致崩溃?
- 为什么 vector 的方法调整大小会创建一个额外的对象?
- 为什么使用 <uint8_t>MSVC 2015 编译器的 std::vector::insert 比 std::copy 快 5 倍?
- 为什么优化大型 std::vector 数组会导致 SegFault?
- 动态分配和随机访问:Raw、Smart、Deque、Vector.为什么生得这么快,而deque却这么慢
- valarray与vector:为什么引入valarray