Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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++中,除了我的问题之外,我如何概括从向量中删除一个元素到一个函数的方法,该函数采用以下参数:向量和要从这个向量中删除的元素? bool removeElementFromVector(vector * collection, void * element) { for(int i=0; i<collection->size(); i++){ if (collection[i]==element){ swap(collection[i], collection.back()); collection.pop_back(); return true; } } }_C++ - Fatal编程技术网

从函数中的向量中删除元素 在C++中,除了我的问题之外,我如何概括从向量中删除一个元素到一个函数的方法,该函数采用以下参数:向量和要从这个向量中删除的元素? bool removeElementFromVector(vector * collection, void * element) { for(int i=0; i<collection->size(); i++){ if (collection[i]==element){ swap(collection[i], collection.back()); collection.pop_back(); return true; } } }

从函数中的向量中删除元素 在C++中,除了我的问题之外,我如何概括从向量中删除一个元素到一个函数的方法,该函数采用以下参数:向量和要从这个向量中删除的元素? bool removeElementFromVector(vector * collection, void * element) { for(int i=0; i<collection->size(); i++){ if (collection[i]==element){ swap(collection[i], collection.back()); collection.pop_back(); return true; } } },c++,C++,myclass.h #include "myfunctions.h" public: vector<Item*> items; void removeItem(Item * item); 应将函数制作成模板: template <typename T> bool removeElementFromVector(vector<T> & collection, T const &

myclass.h

#include "myfunctions.h"
public:
vector<Item*> items;                        
void removeItem(Item * item);          

应将函数制作成模板:

template <typename T>
bool removeElementFromVector(vector<T> & collection, T const & element);
template <typename T>
bool removeElementFromVector(vector<T*> * collection, T* element) {
    for(int i=0; i<collection->size(); i++){
        if (collection[i]==element){
            swap(collection[i], collection.back());
            collection.pop_back();
            return true;
        }
    }
}
模板
bool removelementfromVector(向量和集合、T常量和元素);

另外,不要使用指针。

您应该将函数制作成模板:

template <typename T>
bool removeElementFromVector(vector<T> & collection, T const & element);
template <typename T>
bool removeElementFromVector(vector<T*> * collection, T* element) {
    for(int i=0; i<collection->size(); i++){
        if (collection[i]==element){
            swap(collection[i], collection.back());
            collection.pop_back();
            return true;
        }
    }
}
模板
bool removelementfromVector(向量和集合、T常量和元素);

另外,不要使用指针。

将函数作为模板:

template <typename T>
bool removeElementFromVector(vector<T> & collection, T const & element);
template <typename T>
bool removeElementFromVector(vector<T*> * collection, T* element) {
    for(int i=0; i<collection->size(); i++){
        if (collection[i]==element){
            swap(collection[i], collection.back());
            collection.pop_back();
            return true;
        }
    }
}
模板
boolRemoveElementFromVector(向量*集合,T*元素){
对于(int i=0;isize();i++){
if(集合[i]==元素){
swap(collection[i],collection.back());
collection.pop_back();
返回true;
}
}
}

另一方面,您的代码使用这些指针相当糟糕。标准容器用于存储完整的对象,而不仅仅是指针。同样地,
元素
参数也可以很容易地成为(const)引用。

将函数作为模板:

template <typename T>
bool removeElementFromVector(vector<T> & collection, T const & element);
template <typename T>
bool removeElementFromVector(vector<T*> * collection, T* element) {
    for(int i=0; i<collection->size(); i++){
        if (collection[i]==element){
            swap(collection[i], collection.back());
            collection.pop_back();
            return true;
        }
    }
}
模板
boolRemoveElementFromVector(向量*集合,T*元素){
对于(int i=0;isize();i++){
if(集合[i]==元素){
swap(collection[i],collection.back());
collection.pop_back();
返回true;
}
}
}

另一方面,您的代码使用这些指针相当糟糕。标准容器用于存储完整的对象,而不仅仅是指针。同样,元素参数很容易成为(const)引用,C++中的

,编写不同类型的通用代码的类型安全的方法不通过“代码> Value*/Cuff>”,而是“模板”。在您的特殊情况下:

template <typename T>
void removeElement( std::vector<T> & collection, T const & element ) {
   collection.erase( std::remove( collection.begin(), collection.end(), element ),
                     collection.end() );
}
模板
void removelement(std::vector&collection、T const&element){
collection.erase(std::remove(collection.begin()、collection.end()、element),
collection.end());
}
通过在包含的类型
T
上使用模板,可以使其成为泛型。在内部,从向量中删除元素的习惯用法是erase-remove习惯用法,它将删除匹配的元素,并按相对顺序向前压缩其余元素。我已经更改了引用的指针。如果您的容器包含指向给定类型的指针,并且传递的元素是指向该类型的指针,那么编译器将为您推断
T
type*
,但上面的代码也适用于不包含指针的容器(更一般一些)


如果相对顺序不重要,可以使用与问题相关的相同循环,这将更有效(拷贝数较少)。C++中的

< P>,对不同类型工作的泛型代码的类型安全方式不通过“代码> Value*/COD>”,而是“模板”。在您的特殊情况下:

template <typename T>
void removeElement( std::vector<T> & collection, T const & element ) {
   collection.erase( std::remove( collection.begin(), collection.end(), element ),
                     collection.end() );
}
模板
void removelement(std::vector&collection、T const&element){
collection.erase(std::remove(collection.begin()、collection.end()、element),
collection.end());
}
通过在包含的类型
T
上使用模板,可以使其成为泛型。在内部,从向量中删除元素的习惯用法是erase-remove习惯用法,它将删除匹配的元素,并按相对顺序向前压缩其余元素。我已经更改了引用的指针。如果您的容器包含指向给定类型的指针,并且传递的元素是指向该类型的指针,那么编译器将为您推断
T
type*
,但上面的代码也适用于不包含指针的容器(更一般一些)


如果相对顺序不重要,您可以使用您在问题中使用的相同循环,这将更有效(副本数更少)。

vector类看起来像什么?vector肯定会采用类似vector->erase(元素)的方式;例如,@Valmond,
vector::erase
O(n)
,OP的方法是
O(1)
(尽管它扰乱了元素的顺序)。OP不关心顺序;)向量类看起来像什么?vector肯定会采用类似vector->erase(元素)的方式;例如,@Valmond,
vector::erase
O(n)
,OP的方法是
O(1)
(尽管它扰乱了元素的顺序)。OP不关心顺序;)为什么我应该使用引用而不是指针?我尝试如下使用:
向量项;移除(项目,项目)但我得到了编译器错误:对
bool removeElement(std::vector&,Item*const&)的未定义引用
有任何帮助吗?@Ben:“为什么我应该使用引用而不是指针?”引用更容易正确使用,因为您不必处理它们为null的可能性,而且你不能意外地更改它们引用的对象。@Ben:“有什么帮助吗?”我猜你把模板定义放在了源文件中。你必须把它放在一个头文件中,并从任何使用它的文件中包含它。@Mike:Thx,我编辑了我的原始文章,以包含我如何定义和调用它的所有细节。我做错了什么?@Ben:你需要将函数模板的定义从“myfunctions.cpp”移动到“myfunctions.h”。通常,模板定义必须在头文件中,因为它们必须在使用模板的任何地方都可用。为什么我应该使用引用而不是指针?我尝试如下使用:
向量项;移除(项目,项目)但我得到了编译器错误:未定义对
bool removelement(std::vector&,Item*const&)的引用。
有任何帮助吗?@Ben:“为什么我应该使用引用来代替