使用函数内的stoi将String转换为int

时间:2014-10-11 18:37:12

标签: c++ string int hashtable data-conversion

我尝试了多种方法使stoi(或其他函数)能够满足我的需求(将给定的字符串转换为整数,以便为哈希表创建密钥)。具体来说,我想问为什么stoi不喜欢这种转换。我在内存位置0x0034F41C得到错误" std :: invalid_argument。"我环顾四周,无法找到我做错的事。

#include <iostream>
#include <cstdlib>
#include <string> 
using namespace std;


enum EntryType { Legitimate, Empty, Deleted }; //creat new data type that holds node status

struct HashNode //create new struct with (hash node)
{
    string element; // item inside node
    enum EntryType info; //status
};

struct HashTable //Creates a new struct (hash table)
{
    int size; //defines size of table
    HashNode *table; //creates pointer to table
};

int HashFun1(std::string skey, int size) //first hash function
{
    std::string key2 = skey;
    int key = stoi(key2);
    return key % size; // will return the inputed value 
}

//int HashFunc2(string key, int size) //second hash function
//{
//  return(key * size - 1) % size; //needs to convert between string and int
//}



int main()
{
    cout<<HashFun1("t", 2);
}

0 个答案:

没有答案