使用用户定义的类创建一个向量,并在实例化‘ struct std :: _ vector_base ....没有命名‘ value_type’在MyClass类中’

发布于 2025-02-02 03:46:14 字数 857 浏览 2 评论 0原文

我正在创建一个简单的类

#include <iostream>
#include <vector>
#include <algorithm>    

using namespace std;

class MyClass {
    int p;

public:
    MyClass( int q ) { p = q; }
};

,但是当我尝试创建此矢量时,

 vector<int, MyClass> vec1(1);

我的代码断开并引发了此例外,

vector<int,MyClass> vec1(1);

In instantiation of ‘struct std::_Vector_base<int, MyClass>’:
/usr/include/c++/9/bits/stl_vector.h:386:11:   required from ‘class std::vector<int, MyClass>’
vector_class.cpp:19:26:   required from here
/usr/include/c++/9/bits/stl_vector.h:84:21: error: no type named ‘value_type’ in ‘class MyClass’
   84 |  rebind<_Tp>::other _Tp_alloc_type;

我可以解决此手段,我可​​以将int类型与用户定义的vector中的用户类型一起使用,例如this vector&lt; int,myclass&gt;

I am creating a simple class

#include <iostream>
#include <vector>
#include <algorithm>    

using namespace std;

class MyClass {
    int p;

public:
    MyClass( int q ) { p = q; }
};

but when I try to create this vector

 vector<int, MyClass> vec1(1);

my code breaks and throws this exception

vector<int,MyClass> vec1(1);

In instantiation of ‘struct std::_Vector_base<int, MyClass>’:
/usr/include/c++/9/bits/stl_vector.h:386:11:   required from ‘class std::vector<int, MyClass>’
vector_class.cpp:19:26:   required from here
/usr/include/c++/9/bits/stl_vector.h:84:21: error: no type named ‘value_type’ in ‘class MyClass’
   84 |  rebind<_Tp>::other _Tp_alloc_type;

Can I resolve this means can I use int type with user defined types in vector like this vector<int,MyClass>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2025-02-09 03:46:14

向量有两个模板参数。

  • 第一个是要存储到向量中的元素类型。
  • 第二个是分配器,是向量使用的辅助类别,可分配和释放存储器。

您正在尝试创建一个使用myClass作为分配器的向量。但是,myClass不提供vector期望的呼叫,因此它无法编译。

您想完成什么?

如果您想要一个包含myClass对象的向量,则应声明vector&lt; myClass&gt;。如果您想要一个包含对象对的向量,一个int和myClass,则可以使用vector&lt; pair&lt; int,myclass&gt;(如其他注释者所述) 。

但是,从您的问题中尚不清楚您真正想要什么。

vector has two template parameters.

  • The first is the type of elements to be stored into the vector.
  • The second is an allocator, a helper class that allocates and frees memory used by the vector.

You are trying to create a vector that uses MyClass as the allocator. However, MyClass does not provide the calls that the vector expects, so it fails to compile.

What are you trying to accomplish?

If you want a vector containing objects of MyClass, then you should declare a vector<MyClass>. If you want a vector that contains pairs of objects, an int and a MyClass then you can use vector<pair<int, MyClass> (as the other commenters have mentioned).

But it is not clear from your question what you really want.

七颜 2025-02-09 03:46:14

如果我在向量中使用对,则如何打印P。

我在向量中使用PAIR ,

class MyClass {
    public:
    int p;

public:
    MyClass( int q ):p(q)
    {
        
    }
};

int main()
{
    std::vector<std::pair<int, MyClass>> vec1{{1, MyClass(44)}, {2,MyClass(45)}, {3,MyClass(46)}};
    
    //iterate through the vector and print data member p
    for(const auto&myPair: vec1)
    {
//-----------------vvvvvvvvvvvvvv-------------->print the data member p 
        std::cout<<myPair.second.p<<std::endl;
    }
}

如果 在线gdb.com/d5gtqjijt“ rel =“ nofollow noreferrer”>工作演示

how to print p if I am using pair in vector as u shown

You can iterate through the pair elements of the vector and print the data member p as shown below:

class MyClass {
    public:
    int p;

public:
    MyClass( int q ):p(q)
    {
        
    }
};

int main()
{
    std::vector<std::pair<int, MyClass>> vec1{{1, MyClass(44)}, {2,MyClass(45)}, {3,MyClass(46)}};
    
    //iterate through the vector and print data member p
    for(const auto&myPair: vec1)
    {
//-----------------vvvvvvvvvvvvvv-------------->print the data member p 
        std::cout<<myPair.second.p<<std::endl;
    }
}

Working demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文