专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 vector赋值的方法

vector赋值的方法

更新时间:2022-12-28 12:00:55 来源:赢咖4 浏览354次

vector赋值的方法是什么?赢咖4小编来告诉大家。

vector间赋值不能使用memcpy_s,会导致原来变量的内存出错(猜的)。

可使用循环赋值获assign方法,assign方法效率较高。

static vector<string> vec_test(1000000, "无力的反垄断的积分辣豆腐");
int main()
{
    vector<string> v1;
    vector<string> v2;
    vector<string> v3;     
    int t1,t2; 
    t1 = GetTickCount();
    for (int i = 0; i < 1000000; ++i)
    {
        v1.push_back(vec_test[i]);
    }
    t2 = GetTickCount();
    cout << t2 - t1 << endl; 
    t1 = GetTickCount();
    vector<string>::iterator itr;
    for (itr = vec_test.begin(); itr != vec_test.end(); ++itr)
    {
        v2.push_back(*itr);
    }
    t2 = GetTickCount();
    cout << t2 - t1 << endl; 
    t1 = GetTickCount();
    v3.assign(vec_test.begin(), vec_test.end());
    t2 = GetTickCount();
    cout << t2 - t1 << endl;
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>