Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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+引用类向量的元素+;_C++_Class_Pointers_Object_Vector - Fatal编程技术网

C++ 使用C+引用类向量的元素+;

C++ 使用C+引用类向量的元素+;,c++,class,pointers,object,vector,C++,Class,Pointers,Object,Vector,我试图使用指针切换类向量的元素。我不是简单地用这个方法来解决一个小问题,而是为了练习用这个方法解决更难的问题 这是我的密码 #include <iostream> #include <algorithm> #include <vector> using namespace std; class thing{ public: int index; int value; thing(); priva

我试图使用指针切换类向量的元素。我不是简单地用这个方法来解决一个小问题,而是为了练习用这个方法解决更难的问题

这是我的密码

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class thing{
    public:
        int index;
        int value;
        thing();
    private: int number;

};
thing::thing()
{
    number = 0;
}
void arrange(vector<thing>* array[]){
    for(int i=0; i<19; ++i){
        if(array[i]->value<array[i+1]->value){
            swap(array[i], array[i+1]);
            arrange(array);
        }
    }
}
int main(){
    vector<thing>** things = new thing*[20];
    for (int i=0; i < 20; ++i)
    {
        things[i] = new thing();  // default constructor
        things[i]->index = i;
        things[i]->value=rand() % 100;
    }
    cout << "The random array is: " << endl;
    for(int i=0;i<20;++i){
        cout << things[i]->value << endl;
    }
    arrange(*things);
    cout << "The arranged array is: " << endl;
    for (int i=0; i < 20; ++i)
    {
        cout << things[i]->value << endl;
    }
    return 0;

}
#包括
#包括
#包括
使用名称空间std;
阶级事务{
公众:
整数指数;
int值;
事物();
私人:整数;
};
thing::thing()
{
数字=0;
}
空排列(向量*数组[]){
对于(int i=0;ivaluevalue){
交换(数组[i],数组[i+1]);
排列(数组);
}
}
}
int main(){
向量**事物=新事物*[20];
对于(int i=0;i<20;++i)
{
things[i]=newthing();//默认构造函数
事物[i]->index=i;
事物[i]->value=rand()%100;
}

我想这就是你想要的:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class thing{
    public:
    int index;
    int value;
    thing();
    private: int number;

};
thing::thing()
{
    number = 0;
}
// This arrange accept array of any length
void arrange(vector<thing *> &array)
{
    while(1)
    {
        int regressed=1;
        for(int i=0; i<array.size()-1; i++){
        if( (array[i]->value) < (array[i+1]->value) ){
            thing * tmp=array[i+1];
            array[i+1]=array[i];
            array[i]=tmp;
            regressed=0;
        }
        }
        if(regressed==1)
            return;
        }
}
int main(){
    vector<thing*> things; // init a container to contain " thing * " type vars
    for (int i=0; i < 20; ++i)
    {
    thing * t = new thing();  // default constructor
    t->index = i;
    t->value=rand() % 100;
    things.push_back(t);      // Add vars appended
    }
    cout << "The random array is: " << endl;
    for(int i=0;i<20;++i){
    cout << things[i]->value << endl;
    }
    arrange(things);
    cout << "The arranged array is: " << endl;
    for (int i=0; i < 20; ++i)
    {
    cout << things[i]->value << endl;
    }
    return 0;

}
记住:

vector<int> varname;
vector<int *> varname;
但是,在向向量中添加新元素时,向量可以自动展开:

vectVar.push_back(sth);

首先阅读一些关于vector用法的文档会很有帮助。

一个更明智的问题是为什么。指向vector指针的指针?不寒而栗,有很多更好的方法可以解决这个问题。只是练习一下。另外,我认为使用这种方法使用的内存更少。不要混合使用向量和手动分配的动态数组。你会得到b的最坏结果其他世界。你有什么问题?你告诉我们的只是你在尝试做什么。我在你的帖子中没有看到任何问题。是的,这正是我想要的,谢谢。但第二部分不起作用排列函数不起作用,程序暂停。这可能是一个无限循环吗?输出如你所示,但排列a没有输出我已经为你的困惑更新了答案。如果你看到这个答案,请检查上面的答案。我非常感谢你,解决了所有问题。我唯一做的更改是*tmp=array[I+1];array[I+1]=array[I];array[I]=tmp;我用简单交换(array[I],array[I+1]);恭喜!如果问题已解决,请选择此项作为解决此问题的“答案”。
int varname[size];
int* varname[size];
vectVar.push_back(sth);