如何将一个元素从二维向量数组的特定位置提取到另一个特定位置的二维向量数组中 #包括 #包括 使用名称空间std; int main(){ //第一个问题 向量样本{“你好”},{“世界”}; 病媒试验; 测试[0][0]=样本[0][0];//不起作用 测试[0][0]。将_向后推(示例[0][0]);//仍不起作用 test.insert(0,0,示例[0][0]);//也不起作用 //第二个问题 测试[1][0]。向后推(“Hello”);//不起作用 //什么都不管用 //怎么样????????? } 我试图编写一个C++程序,它将一个元素从一个向量的特定位置复制到向量的另一个特定位置。示例:我想将元素示例[0][0]复制到测试[0][0],它是“Hello”。我尝试了上述方法,但没有任何效果。 第二个问题是,如果我想将样本[0][1]复制到测试[100][100],如何复制
附:错误的副本如何将一个元素从二维向量数组的特定位置提取到另一个特定位置的二维向量数组中 #包括 #包括 使用名称空间std; int main(){ //第一个问题 向量样本{“你好”},{“世界”}; 病媒试验; 测试[0][0]=样本[0][0];//不起作用 测试[0][0]。将_向后推(示例[0][0]);//仍不起作用 test.insert(0,0,示例[0][0]);//也不起作用 //第二个问题 测试[1][0]。向后推(“Hello”);//不起作用 //什么都不管用 //怎么样????????? } 我试图编写一个C++程序,它将一个元素从一个向量的特定位置复制到向量的另一个特定位置。示例:我想将元素示例[0][0]复制到测试[0][0],它是“Hello”。我尝试了上述方法,但没有任何效果。 第二个问题是,如果我想将样本[0][1]复制到测试[100][100],如何复制,c++,C++,附:错误的副本 #include <bits/stdc++.h> #include <vector> using namespace std; int main(){ //first question vector<vector<string> > sample{ {"Hello"}, {"World"} }; vector<vector<string> > test; test[0][0]=
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main(){
//first question
vector<vector<string> > sample{ {"Hello"}, {"World"} };
vector<vector<string> > test;
test[0][0]=sample[0][0]; //doesnt work
test[0][0].push_back(sample[0][0]); //still doesnt work
test.insert(0,0,sample[0][0]); //also doesnt work
//second question
test[1][0].push_back("Hello"); //doesnt work
//nothing works
//how?????????
}
[test.cc 2020-02-29 12:51:27.385]
、test.cc:在函数“int main()”中:
test.cc:5:75:错误:调用“std::vector::vector()”时没有匹配的函数
5 |向量样本{{1,2,3},{4,5,6},{7,8,9};
| ^
在/usr/include/c++/9/vector:67中包含的文件中,
来自/usr/include/c++/9/functional:62,
从/usr/include/c++/9/pstl/glue_算法定义h:13,
来自/usr/include/c++/9/算法:71,
从/usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
来自测试。cc:1:
/usr/include/c++/9/bits/stl_vector.h:650:2:注意:候选:“模板std::vector::vector(_InputIterator,_InputIterator,const分配器类型&)”
650 |向量(_inputierator uu优先,_inputierator u最后,
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:650:2:注意:模板参数推导/替换失败:
test.cc:5:75:注意:无法推断模板参数“\u InputIterator”
5 |向量样本{{1,2,3},{4,5,6},{7,8,9};
| ^
在/usr/include/c++/9/vector:67中包含的文件中,
来自/usr/include/c++/9/functional:62,
从/usr/include/c++/9/pstl/glue_算法定义h:13,
来自/usr/include/c++/9/算法:71,
从/usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
来自测试。cc:1:
/usr/include/c++/9/bits/stl_vector.h:622:7:注:候选:'std::vector::vector(std::initializer_list,const allocator_type&)[with(with)Tp=std::vector;_Alloc=std::allocator;std::vector::allocator(type=std::allocator]'
622向量(初始值设定项列表),
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:622:7:注意:候选者需要2个参数,提供3个
/usr/include/c++/9/bits/stl_vector.h:604:7:注:候选:'std::vector::vector(std::vector&&,const allocator_type&)[with _Tp=std::vector;_Alloc=std::allocator;std::vector::allocator_type=std::allocator]'
604 |向量(向量和向量,常量分配器类型和向量)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:604:7:注意:候选者需要2个参数,提供3个
/usr/include/c++/9/bits/stl_vector.h:586:7:注:候选:'std::vector::vector(std::vector&&,const allocator_type&,std::false_type)[with Tp=std::vector;Alloc=std::alloctor;std::vector::allocator_type=std::allocator;std::false_type=std::integral_常量]'
586 |向量(向量和&&u rv,常量分配器类型和u m,假类型)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:586:23:注意:参数1从“”到“std::vector&”的转换未知
586 |向量(向量和&&u rv,常量分配器类型和u m,假类型)
| ~~~~~~~~~^~~~
/usr/include/c++/9/bits/stl_vector.h:582:7:注:候选:'std::vector::vector(std::vector&&,const allocator_type&,std::true_type)[with Tp=std::vector;Alloc=std::allocator;std::vector::allocator_type=std::allocator;std::true_type=std::integral_常量]'
582 | vector(vector&&uuu rv,常量分配器类型&&uu m,true类型)无例外
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:582:23:注意:参数1从“”到“std::vector&”的转换未知
582 | vector(vector&&uuu rv,常量分配器类型&&uu m,true类型)无例外
| ~~~~~~~~~^~~~
/usr/include/c++/9/bits/stl_vector.h:572:7:注:候选者:“std::vector::vector(const std::vector&,const allocator_type&)[with _Tp=std::vector;_Alloc=std::allocator;std::vector::allocator_type=std::allocator]”
572 |向量(常量向量和常量分配程序类型和常量分配程序类型)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:572:7:注意:候选者需要2个参数,提供3个
/usr/include/c++/9/bits/stl_vector.h:569:7:注:候选:“std::vector::vector(std::vector&)[with _Tp=std::vector;_Alloc=std::alloctor]”
569 | vector(vector&&)noexcept=默认值;
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:569:7:注意:候选者需要1个参数,提供3个
/usr/include/c++/9/bits/stl_vector.h:550:7:注:候选:'std::vector::vector(const std::vector&)[with _Tp=std::vector;_Alloc=std::allocator]'
550 |向量(常数向量和| x)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:550:7:注意:候选者需要1个参数,提供3个
/usr/include/c++/9/bits/stl_vector.h:519:7:注:候选:'std::vector::vector(std::vector::size_type,const value_type&,const allocator_type&)[带std=std::vector;\u Alloc=std::allocator;std::vector::size_type=long unsigned int;std::vector::value_type=std::vector;std::vector::allocator\u type=std::allocator]
519矢量(大小、类型、常数值、类型和值、,
| ^~~~~~
/u
[test.cc 2020-02-29 12:51:27.385]
,,test.cc: In function ‘int main()’:
test.cc:5:75: error: no matching function for call to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >::vector(<brace-enclosed initializer list>)’
5 | vector<vector<string> > sample{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
| ^
In file included from /usr/include/c++/9/vector:67,
from /usr/include/c++/9/functional:62,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
from test.cc:1:
/usr/include/c++/9/bits/stl_vector.h:650:2: note: candidate: ‘template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)’
650 | vector(_InputIterator __first, _InputIterator __last,
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:650:2: note: template argument deduction/substitution failed:
test.cc:5:75: note: couldn’t deduce template parameter ‘_InputIterator’
5 | vector<vector<string> > sample{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
| ^
In file included from /usr/include/c++/9/vector:67,
from /usr/include/c++/9/functional:62,
from /usr/include/c++/9/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/9/algorithm:71,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
from test.cc:1:
/usr/include/c++/9/bits/stl_vector.h:622:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
622 | vector(initializer_list<value_type> __l,
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:622:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/9/bits/stl_vector.h:604:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
604 | vector(vector&& __rv, const allocator_type& __m)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:604:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/9/bits/stl_vector.h:586:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::false_type) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::false_type = std::integral_constant<bool, false>]’
586 | vector(vector&& __rv, const allocator_type& __m, false_type)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:586:23: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >&&’
586 | vector(vector&& __rv, const allocator_type& __m, false_type)
| ~~~~~~~~~^~~~
/usr/include/c++/9/bits/stl_vector.h:582:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::true_type) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::true_type = std::integral_constant<bool, true>]’
582 | vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:582:23: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >&&’
582 | vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
| ~~~~~~~~~^~~~
/usr/include/c++/9/bits/stl_vector.h:572:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, const allocator_type&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
572 | vector(const vector& __x, const allocator_type& __a)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:572:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/9/bits/stl_vector.h:569:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
569 | vector(vector&&) noexcept = default;
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:569:7: note: candidate expects 1 argument, 3 provided
/usr/include/c++/9/bits/stl_vector.h:550:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
550 | vector(const vector& __x)
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:550:7: note: candidate expects 1 argument, 3 provided
/usr/include/c++/9/bits/stl_vector.h:519:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::vector<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
519 | vector(size_type __n, const value_type& __value,
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:519:24: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >::size_type’ {aka ‘long unsigned int’}
519 | vector(size_type __n, const value_type& __value,
| ~~~~~~~~~~^~~
/usr/include/c++/9/bits/stl_vector.h:507:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
507 | vector(size_type __n, const allocator_type& __a = allocator_type())
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:507:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/9/bits/stl_vector.h:494:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
494 | vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:494:7: note: candidate expects 1 argument, 3 provided
/usr/include/c++/9/bits/stl_vector.h:484:7: note: candidate: ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = std::vector<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::vector<std::__cxx11::basic_string<char> > >]’
484 | vector() = default;
| ^~~~~~
/usr/include/c++/9/bits/stl_vector.h:484:7: note: candidate expects 0 arguments, 3 provided
[test.cc 2020-02-29 12:54:15.771]
,,test.cc: In function ‘int main()’:
test.cc:5:5: error: ‘test’ was not declared in this scope
5 | test[1][0].push_back("Hello");
| ^~~~
test[0][0]=sample[0][0];
test[0][0].push_back(sample[0][0]);
test.insert(0,0,sample[0][0]);
test[1][0].push_back("Hello");
std::vector<std::vector<std::string>>> test(1); // initialize with 1 element
test[0].push_back(sample[0][0]); // append element from sample[0][0] to the first vector
// since the index is zero-based you need a vector of
// 101 elements to access 100th element
test.resize(101);
for (auto &inner : test)
inner.resize(101);
// now you can do this
test[100][100] = sample[0][1];
test.push_back({}); // add empty vector to the outer vector
test[0].push_back(sample[0][0]); // add your string to the first inner vector
vector<vector<string> > sample{ {"Hello"}, {"World"} };
vector<vector<string> > test;
std::vector<std::string> sample = {"a", "b", "c"};
std::vector<std::string> test;
test.push_back(sample[0]);
std::cout<<test[0]<<"\n";
std::vector<std::vector<std::string>> sample = {{"a", "b"}, {"c", "d"}};
std::vector<std::vector<std::string>> test;
// Make 'test' correct dimension
test.resize(2); //makes 'test' a vector of vectors of size 2
for (auto &t : test){
t.resize(2); //makes each element of test (each vector) a vector of size 2
}
test[0][0] = sample[0][0];
std::cout << test[0][0] << "\n";