看似空矢量 [英] Seemingly empty vector

查看:150
本文介绍了看似空矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个简单的c ++程序添加了一些轻微的多线程,并且遇到了一些问题。

I've added some slight multi threading to a simple c++ program and have encountered a few issues along the way.

这些问题最新的是 historical :: assignthreads 是从函数 historical :: writeData 中接收到一个空向量。

The latest of these issues is that historical::assignthreads for some reason is receiving an empty vector from the function historical::writeData.

下面的代码将会看到writeData通过一个向量迭代,并将数据放在占位符中,然后再发送给assignthreads(在5次迭代之后),这意味着从writeData发送到assignthreads的向量不应该为空。

Looking at the code below you will see that writeData iterates through a vector and puts the data in a placeholder before sending it forward to assignthreads (after 5 iterations) - meaning that the vector being sent from writeData to assignthreads shouldn't be empty.

然而在assignthreads中你会看到有两个cout:s,一个在循环之前,一个在循环之后。

However in assignthreads you will see that there are two cout:s, one before and one after the loop. Both writes to cout without the loop even starting.

有没有人知道这是怎么回事?

Does anyone have any idea of how this could be the case?

void historical::writeData(std::vector<std::vector<std::wstring>> in, const string& symbol) {
    std::cout << "Sending data to database connector" << std::endl;
    std::vector<std::vector<std::wstring>> temp;
    std::vector<std::vector<std::wstring>>::iterator it;
    int count = 0;
    for (it = in.begin(); it != in.end(); it++) {
        if (count = 5) {
            cout << "I'm in count 5" << endl;
            assignthreads(temp, symbol);
            temp.clear();
            count = 0;
        }
        else {
            cout << "I'm in count 0" << endl;
            temp.push_back(*it);
            count++;
        }

    }
    if (!temp.empty()) {
        cout << "I'm in empty" << endl;
        assignthreads(temp, symbol);
    }
    else cout << "I'm empty!!" << endl;
}
void historical::assignthreads(std::vector<std::vector<std::wstring>>& partVec, const string& symbol) {
    int i = 0;
    cout << "I'm in assign" << endl;
    vector<thread> threads(size(partVec));
    std::vector<std::vector<std::wstring>>::iterator it;
    for (it = partVec.begin();
         it != partVec.end();
         it++) {
        cout << "I'm in the loop" << endl;
        std::shared_ptr<database_con> sh_ptr(new database_con);
        threads.at(i) = std::thread(&database_con::start, sh_ptr, *it, symbol);
        i++;
    }
    cout << "I've finished" << endl;
    for (auto& th : threads) th.join();

}

void historical::writer(string* pInput) {
    ofstream mf("test.csv");
    if (mf.is_open()) {
        mf << *pInput;
        mf.close();
    }
    else cout << "Unable to open file" << endl;
}


推荐答案

count = 5 是一个赋值,因此总是为真。您打算使用 count == 5

Your fundamental problem here is that count = 5 is an assignment and is therefore always true. You intended to use count == 5.

注意到特别是当你的向量变成大的复制,这是非常浪费,你这样做这两种方式:

It's worth noting that particularly as your vector becomes large copying it is very wasteful, and you're doing this 2 ways:


  1. vector 通过值传递到 writeData ,改为通过引用复制: void writeData(std :: vector< std :: vector< std :: wstring>& in,const string& symbol)

  2. temp 最终将复制中的的每个元素,使用迭代器,因此您的代码必须更改为:

  1. The vector is passed into writeData by value, change to copying by reference: void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol)
  2. temp will eventually copy every element of in, use iterators instead so your code would have to change to:





#define SIZE 5

void assignthreads(std::vector<std::vector<std::wstring>>::iterator start, std::vector<std::vector<std::wstring>>::iterator finish, const string& symbol) {
    cout << "I'm in assign" << endl;

    vector<thread> threads(distance(start, finish));

    for(auto i = 0; start != finish; ++i, ++start) {
        cout << "I'm in the loop" << endl;
        std::shared_ptr<database_con> sh_ptr(new database_con);
        threads.at(i) = std::thread(&database_con::start, sh_ptr, *start, symbol);
    }
    cout << "I've finished" << endl;

    for (auto& th : threads) th.join();
}

void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol) {
    std::cout << "Sending data to database connector" << std::endl;

    auto count = 0;

    while(count < in.size() - SIZE) {
        auto start = next(in.begin(), count);

        count += SIZE;

        auto finish = next(in.begin(), count);

        assignthreads(start, finish, symbol);
    }

    assignthreads(next(in.begin(), count), in.end(), symbol);

    cout << "I'm empty!!" << endl;
}

这篇关于看似空矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆