Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++_Templates_Vector_Copy Constructor - Fatal编程技术网

C++ 复制模板向量

C++ 复制模板向量,c++,templates,vector,copy-constructor,C++,Templates,Vector,Copy Constructor,我当前有一个向量使用我的模板作为其类型: vector<hashData> myTable; 当我尝试执行以下操作时: vector<hashData> oldTable = myTable; vector oldTable=myTable; 我收到此错误消息: 错误C2440:“正在初始化”:无法 从“std::vector”转换为 “std::vector” hashtable.h(211):错误C2440: “正在初始化”:无法从转换 “std::vector

我当前有一个向量使用我的模板作为其类型:

vector<hashData> myTable;
当我尝试执行以下操作时:

vector<hashData> oldTable = myTable;
vector oldTable=myTable;
我收到此错误消息:

错误C2440:“正在初始化”:无法 从“std::vector”转换为 “std::vector”

hashtable.h(211):错误C2440: “正在初始化”:无法从转换 “std::vector”到>“std::vector”之间 [\u Ty=hashTable::hashData]和[\u Ty=unsigned long]

没有构造函数可以采用源类型,或者构造函数重载解析不明确

你知道为什么会发生这种情况吗?我的参考资料似乎认为这是可能的,所以我不确定我的错误在哪里

编辑:这是哈希表实现的完整头文件。对于代码的长度,我深表歉意,但我想包括所有内容,因为我最初的“片段”似乎不够

//驱动程序文件 #包括“hashTable.h”

int main(无效){
//哈希表创建
哈希表newTable(3);
//哈希表插入
newTable.addRecord(5);
newTable.addRecord(6);
newTable.addRecord(7);
newTable.addRecord(8);
}    
//哈希表头文件

// BEGIN HEADER FILE
#ifndef HASHTABLE_H
#define HASHTABLE_H

// Includes (system libraries)
#include <iostream>
#include <vector>

// Includes (custom libraries)

// Namespace
using namespace std;

// hashTable class
template <typename hashType>
class hashTable{
public:
    // constructor
    hashTable(int tableSize, string collisionMode = "Linear"){
        this->myTable.resize(optimizeTableSize(tableSize));
        this->collisionMode = collisionMode;
        this->activeRecords = 0;
    }

    // hashTable operations
    void addRecord(hashType);
    void deleteRecord(hashType);
    pair<bool,int> locateRecordPosition(hashType);
    bool searchRecord(hashType);
    hashType returnRecord(hashType);

    // hashTable mainteance
    void considerRehash();
    void rehashTable();
    int optimizeTableSize(int);

    // hashTable math
    bool isPrime(int);
    int nextPrime(int);

    // collision monitoring
    void collisionLogUpdate(int, string);
    int collisionLogAverage();

    // hash table internal class
    class hashData{
    public:
        // constructor for hashData
        hashData(hashType data){
            this->data = data;
            this->isActive = true;
            this->deleted = false;
        }

        hashData(){
            this->isActive = false;
            this->deleted = false;
        }

        // internal data for hashTable
        hashType data;
        bool deleted;
        bool isActive;
    };

private:
    // hashing function
    int calculateHash(hashType, int);

    // hashTable data structure
    vector<hashData> myTable;
    int activeRecords;

    // collision information
    deque<pair<int, string> > collisionLog;
    string collisionMode;
};

// hashTable implementation
// insert a record into the hash table
template <typename hashType>
void hashTable<hashType>::addRecord(hashType toAdd){
    // search for the record
    pair <bool, int> recordPos = locateRecordPosition(toAdd);

    // analyze the results
    if (recordPos.first == true) // the record already exists and is active
        return;
    // otherwise, go ahead and insert the record at this location
    myTable[recordPos.second] = hashData(toAdd);

    // update our count of active records
    activeRecords++;

    // consider a rehash of the hashTable
    considerRehash();
}

// delete a record from the hash table
template <typename hashType>
void hashTable<hashType>::deleteRecord(hashType toDelete){
    // search for the record
    pair <bool, int> recordPos = locateRecordPosition(toDelete);

    // analyze the results
    if (recordPos.first == false) // the record does not exist -- there is nothing to delete here!
        return;

    // otherwise, go ahead and perform a shallow deletion at this area
    myTable[recordPos.second].deleted = true;

    // update our count of active records
    activeRecords--;

    // consider a rehash of the hashTable
    considerRehash();
}

// find position of record within hash table (if such position exists)
template <typename hashType>
pair<bool,int> hashTable<hashType>::locateRecordPosition(hashType toFind){
    // setup data structures
    int collisionNum = 0;
    unsigned int currentPos;

    // search for the entry within the table
    currentPos = calculateHash(toFind, myTable.size());

    // enter a while loop for checking if we've found the item
    while(myTable.at(currentPos).isActive && !myTable.at(currentPos).deleted){
        // check to see if the entry found at the expected position matches
        if(myTable.at(currentPos).data == toFind){
            // update the collisionLog
            collisionLogUpdate(collisionNum,"locateRecord");

            // return the position of the item
            return pair<bool, int>(true,currentPos); // we've successfully found the item
        }

        // otherwise, we need to look for the correct location
        if (collisionMode == "Quadratic"){
            currentPos += 2 * ++collisionNum - 1;
            if(currentPos >= myTable.size())
                currentPos -= myTable.size();
        }
        else if (collisionMode == "Linear"){
            currentPos += 2 * ++collisionNum - 1;
            if(currentPos >= myTable.size())
                currentPos -= myTable.size();
        }

        // reloop and search again
    }

    // update the collisionLog
    collisionLogUpdate(collisionNum,"locateRecord");

    // if we escaped the loop, we were unable to find the item in the table -- return the first open location
    return pair<bool, int>(false,currentPos); // we didn't find the item
}

// return whether a record exists within hash table
template <typename hashType>
bool hashTable<hashType>::searchRecord(hashType toFind){
    return locateRecordPosition(toFind).first; // we didn't find the item
}

// return the contents of a record from the hash table
template <typename hashType>
hashType hashTable<hashType>::returnRecord(hashType toReturn){
    if (locateRecordPosition(toReturn).first) // if the record actually exists
        return myTable[locateRecordPosition(toReturn).second].data;
    else
        return hashType();
}

// calculate hash value
template <typename hashType>
int hashTable<hashType>::calculateHash(hashType toHash, int tableSize){
    if (toHash < 0) // if we have a negative number, change it prior to hashing
        toHash = (toHash*-1);
    return ((toHash*37) % tableSize);
}

// review the collision log and consider rehashing
template <typename hashType>
void hashTable<hashType>::considerRehash(){
    // check if we have used up more then half of the table, if we have, rehash
    if((activeRecords + 1) > ((signed) myTable.size() / 2))
        rehashTable();

    // check the current average of collisions
    // if the average number of collisions is greater then 20% of the table size (meaning it had to search through 20% of table), rehash
    else if((collisionLogAverage() > (myTable.size() * .20)) && (myTable.size() >= 100))
        rehashTable();

    // check the last operations number of collisions
    // if the number of collisions encounter is greater then 30% of the table size (meaning it had to search through 30% of table), rehash
    else if((collisionLog.back().first > (myTable.size() * .30)) && (myTable.size() >= 100))
        rehashTable();
}

// rehash the table
template <typename hashType>
void hashTable<hashType>::rehashTable(){
    // make a copy of the existing vector
    vector<hashType> oldTable = myTable;

    // reallocate myTable
    myTable.resize(optimizeTableSize(myTable.size() * 2)); // double the size of the current table

    // clear myTable
    myTable.clear();

    // copy the existing table over
    for (unsigned int i = 0; i < oldTable.size(); i++){
        if(oldTable[i].isActive && !oldTable[i].deleted){
            addRecord(oldTable[i].data);
        }
    }
}

// optimze table size
template <typename hashType>
int hashTable<hashType>::optimizeTableSize(int tableSize){
    // if we are performing quadratic probing, we need to optimize the table size to be a prime number, to prevent loops
    if (!isPrime(tableSize)){
        return nextPrime(tableSize);
    }

    // we only need to bother with optimizing the table size IF we are performing quadratic probing
    else
        return tableSize;
}

// determine if prime number
template <typename hashType>
bool hashTable<hashType>::isPrime(int numberToEvaluate){
    if(numberToEvaluate == 0)
        return true;

    numberToEvaluate = abs(numberToEvaluate);

    if(numberToEvaluate % 2 == 0) return true;

    for(int i = 3; i <= sqrt((float)numberToEvaluate); i+=2)
        if(numberToEvaluate % i == 0)
            return false;

    return true;
}

// find the next prime number
template <typename hashType>
int hashTable<hashType>::nextPrime(int numberToEvaluate){
    if (numberToEvaluate % 2 == 0)
        numberToEvaluate++;

    for (; !isPrime(numberToEvaluate); numberToEvaluate+=2)
        ;

    return numberToEvaluate;
}

// update collision log with a new entry
template <typename hashType>
void hashTable<hashType>::collisionLogUpdate(int numberOfCollisions, string operationPerformed){
    // add an entry to the log
    collisionLog.push_back(pair<int,string>(numberOfCollisions, operationPerformed));

    // verify we don't have more then 5 entires, if so, remove them
    while(collisionLog.size() > 5)
        collisionLog.pop_front();
}   

template <typename hashType>
int hashTable<hashType>::collisionLogAverage(){
    // add the last five entries, then take their average
    // the log should be maxed at five entries.. so just add them all

    // average holder
    int average;

    // loop through log
    for (unsigned int i = 0; i < collisionLog.size(); i++){
        average = collisionLog.at(i).first;
    }

    // average the sum
    average = average/5;

    // return the calculated average
    return average;
}   

// END HEADER FILE
#endif
//开始头文件
#ifndef哈希表
#定义哈希表
//包括(系统库)
#包括
#包括
//包括(自定义库)
//名称空间
使用名称空间std;
//哈希表类
模板
类哈希表{
公众:
//建造师
哈希表(int tableSize,string collisionMode=“Linear”){
这->myTable.resize(优化表大小(表大小));
此->碰撞模式=碰撞模式;
此->activeRecords=0;
}
//哈希表操作
void addRecord(hashType);
作废删除记录(哈希类型);
对LocaterRecordPosition(hashType);
布尔搜索记录(hashType);
hashType返回记录(hashType);
//哈希表维护
void considerRehash();
void rehashTable();
int优化表大小(int);
//哈希表数学
bool-isPrime(int);
int nextPrime(int);
//碰撞监测
void collisionLogUpdate(int,string);
int collisionLogAverage();
//哈希表内部类
类哈希数据{
公众:
//hashData的构造函数
hashData(hashType数据){
这->数据=数据;
此->isActive=true;
此->已删除=错误;
}
hashData(){
此->isActive=false;
此->已删除=错误;
}
//哈希表的内部数据
哈希类型数据;
删除bool;
布尔是活跃的;
};
私人:
//散列函数
int calculateHash(hashType,int);
//哈希表数据结构
向量表;
int-activeRecords;
//碰撞信息
德克碰撞测井;
字符串冲突模式;
};
//哈希表实现
//将记录插入哈希表
模板
void hashTable::addRecord(hashType toAdd){
//搜索记录
配对记录位置=位置记录位置(toAdd);
//分析结果
if(recordPos.first==true)//记录已存在且处于活动状态
返回;
//否则,请继续并在此位置插入记录
myTable[recordPos.second]=散列数据(toAdd);
//更新活动记录的计数
activeRecords++;
/考虑哈希表的重新排序。
considerRehash();
}
//从哈希表中删除记录
模板
void hashTable::deleteRecord(hashType toDelete){
//搜索记录
配对记录位置=位置记录位置(toDelete);
//分析结果
if(recordPos.first==false)//记录不存在——这里没有要删除的内容!
返回;
//否则,继续在该区域执行浅层删除
myTable[recordPos.second].deleted=true;
//更新活动记录的计数
活动记录--;
/考虑哈希表的重新排序。
considerRehash();
}
//查找哈希表中记录的位置(如果该位置存在)
模板
成对哈希表::locateRecordPosition(hashType toFind){
//设置数据结构
int collisionNum=0;
无符号int-currentPos;
//在表中搜索条目
currentPos=calculateHash(toFind,myTable.size());
//输入while循环以检查是否找到该项
while(myTable.at(currentPos).isActive&&!myTable.at(currentPos).deleted){
//检查在预期位置找到的条目是否匹配
if(myTable.at(currentPos.data==toFind){
//更新碰撞日志
collisionLogUpdate(collisionNum,“LocaterRecord”);
//返回项目的位置
返回对(true,currentPos);//我们已成功找到该项
}
//否则,我们需要寻找正确的位置
if(碰撞模式==“二次”){
currentPos+=2*++collisionNum-1;
如果(currentPos>=myTable.size())
currentPos-=myTable.size();
}
else if(碰撞模式==“线性”){
currentPos+=2*++collisionNum-1;
如果(currentPos>=myTable.size())
currentPos-=myTable.size();
}
//重新打开并再次搜索
}
//更新碰撞日志
collisionLogUpdate(collisionNum,“LocaterRecord”);
//如果我们跳过了循环,就无法在表中找到该项——返回第一个打开的位置
返回对(false,currentPos);//我们没有找到该项
}
//返回哈希表中是否存在记录
模板
bool hashTable::searchRecord(hashType-toFind){
return locateRecordPosition(toFind).first;//未找到该项
}
//从哈希表返回记录的内容
模板
hashType hashTable::returnRecord(hashType toReturn){
if(locateRecordPosition(toreReturn.first)//如果记录确实存在
雷图
int main(void){
    // hash table creation
    hashTable<unsigned long> newTable(3);

    // hash table insertion
    newTable.addRecord(5);
    newTable.addRecord(6);
    newTable.addRecord(7);
    newTable.addRecord(8);
}    
// BEGIN HEADER FILE
#ifndef HASHTABLE_H
#define HASHTABLE_H

// Includes (system libraries)
#include <iostream>
#include <vector>

// Includes (custom libraries)

// Namespace
using namespace std;

// hashTable class
template <typename hashType>
class hashTable{
public:
    // constructor
    hashTable(int tableSize, string collisionMode = "Linear"){
        this->myTable.resize(optimizeTableSize(tableSize));
        this->collisionMode = collisionMode;
        this->activeRecords = 0;
    }

    // hashTable operations
    void addRecord(hashType);
    void deleteRecord(hashType);
    pair<bool,int> locateRecordPosition(hashType);
    bool searchRecord(hashType);
    hashType returnRecord(hashType);

    // hashTable mainteance
    void considerRehash();
    void rehashTable();
    int optimizeTableSize(int);

    // hashTable math
    bool isPrime(int);
    int nextPrime(int);

    // collision monitoring
    void collisionLogUpdate(int, string);
    int collisionLogAverage();

    // hash table internal class
    class hashData{
    public:
        // constructor for hashData
        hashData(hashType data){
            this->data = data;
            this->isActive = true;
            this->deleted = false;
        }

        hashData(){
            this->isActive = false;
            this->deleted = false;
        }

        // internal data for hashTable
        hashType data;
        bool deleted;
        bool isActive;
    };

private:
    // hashing function
    int calculateHash(hashType, int);

    // hashTable data structure
    vector<hashData> myTable;
    int activeRecords;

    // collision information
    deque<pair<int, string> > collisionLog;
    string collisionMode;
};

// hashTable implementation
// insert a record into the hash table
template <typename hashType>
void hashTable<hashType>::addRecord(hashType toAdd){
    // search for the record
    pair <bool, int> recordPos = locateRecordPosition(toAdd);

    // analyze the results
    if (recordPos.first == true) // the record already exists and is active
        return;
    // otherwise, go ahead and insert the record at this location
    myTable[recordPos.second] = hashData(toAdd);

    // update our count of active records
    activeRecords++;

    // consider a rehash of the hashTable
    considerRehash();
}

// delete a record from the hash table
template <typename hashType>
void hashTable<hashType>::deleteRecord(hashType toDelete){
    // search for the record
    pair <bool, int> recordPos = locateRecordPosition(toDelete);

    // analyze the results
    if (recordPos.first == false) // the record does not exist -- there is nothing to delete here!
        return;

    // otherwise, go ahead and perform a shallow deletion at this area
    myTable[recordPos.second].deleted = true;

    // update our count of active records
    activeRecords--;

    // consider a rehash of the hashTable
    considerRehash();
}

// find position of record within hash table (if such position exists)
template <typename hashType>
pair<bool,int> hashTable<hashType>::locateRecordPosition(hashType toFind){
    // setup data structures
    int collisionNum = 0;
    unsigned int currentPos;

    // search for the entry within the table
    currentPos = calculateHash(toFind, myTable.size());

    // enter a while loop for checking if we've found the item
    while(myTable.at(currentPos).isActive && !myTable.at(currentPos).deleted){
        // check to see if the entry found at the expected position matches
        if(myTable.at(currentPos).data == toFind){
            // update the collisionLog
            collisionLogUpdate(collisionNum,"locateRecord");

            // return the position of the item
            return pair<bool, int>(true,currentPos); // we've successfully found the item
        }

        // otherwise, we need to look for the correct location
        if (collisionMode == "Quadratic"){
            currentPos += 2 * ++collisionNum - 1;
            if(currentPos >= myTable.size())
                currentPos -= myTable.size();
        }
        else if (collisionMode == "Linear"){
            currentPos += 2 * ++collisionNum - 1;
            if(currentPos >= myTable.size())
                currentPos -= myTable.size();
        }

        // reloop and search again
    }

    // update the collisionLog
    collisionLogUpdate(collisionNum,"locateRecord");

    // if we escaped the loop, we were unable to find the item in the table -- return the first open location
    return pair<bool, int>(false,currentPos); // we didn't find the item
}

// return whether a record exists within hash table
template <typename hashType>
bool hashTable<hashType>::searchRecord(hashType toFind){
    return locateRecordPosition(toFind).first; // we didn't find the item
}

// return the contents of a record from the hash table
template <typename hashType>
hashType hashTable<hashType>::returnRecord(hashType toReturn){
    if (locateRecordPosition(toReturn).first) // if the record actually exists
        return myTable[locateRecordPosition(toReturn).second].data;
    else
        return hashType();
}

// calculate hash value
template <typename hashType>
int hashTable<hashType>::calculateHash(hashType toHash, int tableSize){
    if (toHash < 0) // if we have a negative number, change it prior to hashing
        toHash = (toHash*-1);
    return ((toHash*37) % tableSize);
}

// review the collision log and consider rehashing
template <typename hashType>
void hashTable<hashType>::considerRehash(){
    // check if we have used up more then half of the table, if we have, rehash
    if((activeRecords + 1) > ((signed) myTable.size() / 2))
        rehashTable();

    // check the current average of collisions
    // if the average number of collisions is greater then 20% of the table size (meaning it had to search through 20% of table), rehash
    else if((collisionLogAverage() > (myTable.size() * .20)) && (myTable.size() >= 100))
        rehashTable();

    // check the last operations number of collisions
    // if the number of collisions encounter is greater then 30% of the table size (meaning it had to search through 30% of table), rehash
    else if((collisionLog.back().first > (myTable.size() * .30)) && (myTable.size() >= 100))
        rehashTable();
}

// rehash the table
template <typename hashType>
void hashTable<hashType>::rehashTable(){
    // make a copy of the existing vector
    vector<hashType> oldTable = myTable;

    // reallocate myTable
    myTable.resize(optimizeTableSize(myTable.size() * 2)); // double the size of the current table

    // clear myTable
    myTable.clear();

    // copy the existing table over
    for (unsigned int i = 0; i < oldTable.size(); i++){
        if(oldTable[i].isActive && !oldTable[i].deleted){
            addRecord(oldTable[i].data);
        }
    }
}

// optimze table size
template <typename hashType>
int hashTable<hashType>::optimizeTableSize(int tableSize){
    // if we are performing quadratic probing, we need to optimize the table size to be a prime number, to prevent loops
    if (!isPrime(tableSize)){
        return nextPrime(tableSize);
    }

    // we only need to bother with optimizing the table size IF we are performing quadratic probing
    else
        return tableSize;
}

// determine if prime number
template <typename hashType>
bool hashTable<hashType>::isPrime(int numberToEvaluate){
    if(numberToEvaluate == 0)
        return true;

    numberToEvaluate = abs(numberToEvaluate);

    if(numberToEvaluate % 2 == 0) return true;

    for(int i = 3; i <= sqrt((float)numberToEvaluate); i+=2)
        if(numberToEvaluate % i == 0)
            return false;

    return true;
}

// find the next prime number
template <typename hashType>
int hashTable<hashType>::nextPrime(int numberToEvaluate){
    if (numberToEvaluate % 2 == 0)
        numberToEvaluate++;

    for (; !isPrime(numberToEvaluate); numberToEvaluate+=2)
        ;

    return numberToEvaluate;
}

// update collision log with a new entry
template <typename hashType>
void hashTable<hashType>::collisionLogUpdate(int numberOfCollisions, string operationPerformed){
    // add an entry to the log
    collisionLog.push_back(pair<int,string>(numberOfCollisions, operationPerformed));

    // verify we don't have more then 5 entires, if so, remove them
    while(collisionLog.size() > 5)
        collisionLog.pop_front();
}   

template <typename hashType>
int hashTable<hashType>::collisionLogAverage(){
    // add the last five entries, then take their average
    // the log should be maxed at five entries.. so just add them all

    // average holder
    int average;

    // loop through log
    for (unsigned int i = 0; i < collisionLog.size(); i++){
        average = collisionLog.at(i).first;
    }

    // average the sum
    average = average/5;

    // return the calculated average
    return average;
}   

// END HEADER FILE
#endif
vector<hashData> myTable;
vector<hashType> oldTable = myTable;
std::vector<hashData> oldTable = myTable;