C++关于vector和多线程的数据的传递和合并

 

1、 前言 关于vector 的传递的参数的方式

注意,三种方式分别有对应的const形式,不在此讨论。

三种方式对应的调用形式分别为:

function1(vec),传入值
function2(vec),传入引用
function3(&vec),传入地址
三种方式的效果分别为:

会发生拷贝构造
不会发生拷贝构造
不会发生拷贝构造

 

2、实验目标

希望能够将vector传递给下面,并且将下面的vector 中的某些数值 发生改变之后再继续进行使用,将vector进行合并

3、实验diamante

#include <iostream>

#include <thread>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
thread::id main_thread_id = this_thread::get_id();

void hello(int step,vector<double> *feature) 
{
    // ifstream infile("./data/train_data.txt");
    ifstream infile("mydata.txt");
    // ifstream infile("./data/train_data1.txt");

    // vector<double>feature;
    string line,tmp;
    int nums=0;
    int start=clock();
    for(int i=1;i<step;i++){
        getline(infile, line);
    }
    (*feature)[0]= (*feature)[0]*10;

	cout<<"thread"<<step<<"---"<<"nums:"<<nums<<"--time cost:"<<(" %.3lf\n", double(clock() - start) / CLOCKS_PER_SEC)<<endl;;
    // cout<<"thread"<<step<<"---"<<*feature[0]<<endl;


}


int main() {
    // char name[] = "./data/data_train_data.txt";
    ifstream infile("./data/train_data1.txt");
    
   

    // ifstream infile("mydata.txt");

    string line1,line2,line3,line4,tmp,line;
    // char line[]="";
    int i = 0;
	vector<double> feature;
    vector<double> feature1;
    vector<double>feature2;
    vector<double> feature3;
    vector<double> feature4;
	vector<double> a;

    int start=clock();
	
 
    cout<<"we are here1"<<endl;

    //solution one 
    // getline(infile, line1);
    // i++;
    // hello(1,&feature1);


    feature1.push_back(1.2);
    feature2.push_back(2.2);
    feature3.push_back(3.2);
    feature4.push_back(4.2);

    thread t1(hello,1,&feature1);
    thread t2(hello,2,&feature2);
    thread t3(hello,3,&feature3);
    thread t4(hello,4,&feature4);
    
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    cout<<"Feature 1 "<<feature1[0]<<endl;
    cout<<"Feature 2 "<<feature2[0]<<endl;
    cout<<"Feature 3 "<<feature3[0]<<endl;
    cout<<"Feature 4 "<<feature4[0]<<endl;



    
    feature.insert(feature.end(),feature1.begin(),feature1.end());
    feature.insert(feature.end(),feature2.begin(),feature2.end());
    feature.insert(feature.end(),feature3.begin(),feature3.end());
    feature.insert(feature.end(),feature4.begin(),feature4.end());
    cout<<"Feature 1 size "<<feature1.size()<<endl;
    cout<<"Feature 2 size "<<feature2.size()<<endl;
    cout<<"Feature 3 size "<<feature3.size()<<endl;
    cout<<"Feature 4 size "<<feature4.size()<<endl;



    cout<<"this is main "<<feature.size()<<endl;
    for(int j=0;j<feature.size();j++){
        cout<<"this is feature "<<feature[j]<<endl;
        
    }
    

	cout<<" the time cost is"<<(" %.3lf\n", double(clock() - start) / CLOCKS_PER_SEC);

    return 0;
    
}

 

4、 实验结果

 

5、实验结论

(1)对于多线程,要求的传指针,所以结合一下,我们选择传递指针的方案,给子线程

(2)对于vector的指针的调用采用的是(*vec)[0] 这样的方案

(3)关于vector的插入的操作的是vector 自带的insert 的函数的方案。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有水杯和雨伞的工科男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值