-2

I'm trying to create a vector that contain others vectors with a float and other vector of ints. Expected result:

{{5.4 {1, 5, 4, 6, 9},
4.8, {3, 6, 7, 8, 4},
7.3 , {9, 12, 1, 0, 4}}

This is one of my tries:

#include <iostream>
#include <vector>


using namespace std;


void _cost(){
    vector<float, vector<int>> result;

    cout<<"This work";
}

int main(){

    _cost();


    return 0;
}

In all my attemps I receive the following error:

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

I think, the problem is that C++ can't allocate in memory the vector of ints, but I don't know why...

Rubiales Alberto
  • 143
  • 3
  • 12
  • 4
    `vector`'s are homogeneous, i.e. all elements of a single vector should be of the same type. What exactly are you trying to achieve? Would a `struct` or `std::tuple` work for you instead? – yeputons Aug 05 '20 at 22:49
  • Oh, Okay I didn't know that! Thank you very much! – Rubiales Alberto Aug 05 '20 at 22:56
  • 1
    Unrelated: an identifier at global scope that starts with an underscore is reserved for use by "The Implementation". Breaking this rule rarely bites, but when it does, the results can be utterly baffling. More details in [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Aug 05 '20 at 23:02
  • 1
    Should each element of the vector contain a `float` _and_ a `vector`, or should each element of the vector contain a `float` _or_` a `vector`? – Mooing Duck Aug 05 '20 at 23:04
  • yep sorry @user4581301 It was a function of a class. But I only copy the function to avoid unnecessary code! Anyway thank you for the link, there are a lot of knowledge in it :) – Rubiales Alberto Aug 05 '20 at 23:11
  • @MooingDuck Sorry, my example was correct but my title was wront. Yes what I need is a vector that inside have other vector with a float and another vector of ints. I think that MikeCAT answerd is exactly what I need. – Rubiales Alberto Aug 05 '20 at 23:16

3 Answers3

5

It seems you want to create a vector of structures that have float and vector of 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;
}

Or a vector of pairs:

#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;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
2

Something like this should work:

#include <iostream>
#include <variant>
#include <vector>

int main() {
  std::vector<std::variant<float, std::vector<int>>> v;
  v.push_back(1.0f);
  v.push_back(std::vector<int>{1, 2, 3});

  std::cout << std::get<float>(v[0]) << std::endl;
  std::cout << std::get<std::vector<int>>(v[1])[1] << std::endl;

  return 0;
}

It uses the std::variant, which is a type-safe union.

Here's the output of the code when we compile and run it:

% g++ -std=c++17 vec.cc && ./a.out
1
2

(I wanted to print the vector, but I don't know C++ all that well.)

Julia
  • 1,950
  • 1
  • 9
  • 22
2

It looks like @MikeCAT has already given a reasonable answer for how you create working code.

So for the moment I'm going to concentrate on why your code didn't work, and what the error message is trying to tell you.

The type signature for std::vector is like this:

template<class T, class Allocator = allocator<T>>
class vector {

So with your code: vector<float, vector<int>> result;, you're passing float as the type of the object that will be contained in the vector, and vector<int> as the type for the Allocator the vector will use.

An Allocator is what a collection uses to allocate space for the data it stores. To do that, it's required to have (among other things) members named allocate, and deallocate. The error message is telling you that std::vector doesn't have those, so it can't be used as an Allocator.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111