我正在尝试创建一个向量,其中包含带有浮点数和其他整数向量的其他向量。预期结果:
{{5.4 {1, 5, 4, 6, 9},
4.8, {3, 6, 7, 8, 4},
7.3 , {9, 12, 1, 0, 4}}
Run Code Online (Sandbox Code Playgroud)
这是我的尝试之一:
#include <iostream>
#include <vector>
using namespace std;
void _cost(){
vector<float, vector<int>> result;
cout<<"This work";
}
int main(){
_cost();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我所有的尝试中,我收到以下错误:
In file included from /usr/include/c++/7/ext/alloc_traits.h:36:0,
from /usr/include/c++/7/bits/basic_string.h:40,
from /usr/include/c++/7/string:52,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from prueba_dela_prueba.cpp:1:
/usr/include/c++/7/bits/alloc_traits.h: In instantiation of ‘static void std::allocator_traits<_Alloc>::deallocate(_Alloc&, std::allocator_traits<_Alloc>::pointer, std::allocator_traits<_Alloc>::size_type) [with _Alloc = std::vector<float, std::allocator<int> >; std::allocator_traits<_Alloc>::pointer = float*; std::allocator_traits<_Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/stl_vector.h:180:19: required from ‘void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(std::_Vector_base<_Tp, _Alloc>::pointer, std::size_t) [with _Tp = float; _Alloc = std::vector<int>; std::_Vector_base<_Tp, _Alloc>::pointer = float*; std::size_t = long unsigned int]’
/usr/include/c++/7/bits/stl_vector.h:162:22: required from ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = float; _Alloc = std::vector<int>]’
/usr/include/c++/7/bits/stl_vector.h:263:15: required from ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = float; _Alloc = std::vector<int>]’
prueba_dela_prueba.cpp:9:32: required from here
/usr/include/c++/7/bits/alloc_traits.h:328:13: error: ‘class std::vector<float, std::allocator<int> >’ has no member named ‘deallocate’; did you mean ‘_M_deallocate’?
{ __a.deallocate(__p, __n); }
~~~~^~~~~~~~~~
_M_deallocate
Run Code Online (Sandbox Code Playgroud)
我认为,问题是 C++ 不能在内存中分配整数向量,但我不知道为什么......
似乎您想创建一个具有float
和 向量的结构向量int
;
#include <iostream>
#include <vector>
struct my_data {
float hoge;
std::vector<int> fuga;
};
int main() {
std::vector<my_data> result = {
{5.4 , {1, 5, 4, 6, 9}},
{4.8 , {3, 6, 7, 8, 4}},
{7.3 , {9, 12, 1, 0, 4}}
};
for (auto& d : result) {
std::cout << "[" << d.hoge << "]";
for (auto& i : d.fuga) {
std::cout << ", " << i;
}
std::cout << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或成对的向量:
#include <iostream>
#include <vector>
#include <utility>
int main() {
std::vector<std::pair<float, std::vector<int> > > result = {
{5.4 , {1, 5, 4, 6, 9}},
{4.8 , {3, 6, 7, 8, 4}},
{7.3 , {9, 12, 1, 0, 4}}
};
for (auto& d : result) {
std::cout << "[" << d.first << "]";
for (auto& i : d.second) {
std::cout << ", " << i;
}
std::cout << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
225 次 |
最近记录: |