Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制到矢量故障_C++ - Fatal编程技术网

C++ 复制到矢量故障

C++ 复制到矢量故障,c++,C++,我正在尝试将向量数据从sample复制到Y,如下所示 std::map<std::string, std::vector<double > >sample; std::map<std::string, std::vector<double > >::iterator it1=sample.begin(), end1=sample.end(); std::vector<double> Y; 我只想将it1->second的内容复制到Y

我正在尝试将向量数据从
sample
复制到
Y
,如下所示

std::map<std::string, std::vector<double > >sample;
std::map<std::string, std::vector<double > >::iterator it1=sample.begin(), end1=sample.end();
std::vector<double> Y; 

我只想将it1->second的内容复制到Y。为什么它不起作用?我该如何修复它?

显然,你想在向量中插入对象。但是,
std::copy()
只接受传递的迭代器并将其写入。由
begin()
end()
迭代器获得的迭代器不进行任何插入。您想要使用的是如下内容:

std::copy(it1->second.begin(), it1->second.end(), std::back_inserter(Y));
std::back\u inserter()
函数模板是迭代器的一个工厂函数,在其参数上使用
push\u back()
来附加对象。

\35; include
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    // your code goes here
    vector<int> vec;
    vector<int> test;
    vec.push_back(1);
    //test.push_back(0);
    copy(vec.begin(),vec.begin()+1,test.begin());
    cout << *(test.begin());
    return 0;
}
#包括 #包括 使用名称空间std; int main(){ //你的密码在这里 向量向量机; 病媒试验; 向量推回(1); //测试。推回(0); 复制(vec.begin(),vec.begin()+1,test.begin());
很好的捕获。仔细想想,空迭代器范围不能被取消引用是有道理的。
std::copy(it1->second.begin(), it1->second.end(), std::back_inserter(Y));
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    // your code goes here
    vector<int> vec;
    vector<int> test;
    vec.push_back(1);
    //test.push_back(0);
    copy(vec.begin(),vec.begin()+1,test.begin());
    cout << *(test.begin());
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    // your code goes here
    vector<int> vec;
    vector<int> test;
    vec.push_back(1);
    test.push_back(0);
    copy(vec.begin(),vec.begin()+1,test.begin());
    cout << *(test.begin());
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    // your code goes here
    vector<int> vec;
    vector<int> test(5);
    vec.push_back(1);
    //test.push_back(0);
    copy(vec.begin(),vec.begin()+1,test.begin());
    cout << *(test.begin());
    return 0;
}