在c++中将字符串转换为字符

Conversion of string to char in c++

本文关键字:转换 字符 字符串 c++      更新时间:2023-10-16

我正在尝试使用哈希表构建一个拼写检查验证器。我有一个文本文件中的单词列表。我想将它们导入到程序中,并使用单独的链接将它们输入到哈希表中。现在,我想运行程序,但我有这两个错误。有人能帮我吗?

第30行的

--没有用于调用的匹配函数'std::__cxx11::basic_string::push_back(std::__ cxx11:;string&('

在第40行--"operator=="没有匹配项(操作数类型为'__gnu_cxx::__alloc_traits,char>::value_type'{aka"char"}和"std::__cxx11::string"'std::__cxx11::basic_string'}(

我知道将str转换为char是一个简单的错误,但我不知道如何在不更改程序其余部分的情况下完成。

我希望有一个简单的解决方案,不改变现有的代码。如果不可能,请告诉我如何进行。

#include<iostream>
#include <string>
#include <cstring>
#include <fstream>

std::string hashTable[27];
int hashTableSize = 27;
#define MAX_LEN 27
using namespace std;

int hashFunc(std::string s)
{
// A simple hashing, no collision handled
int sum=0,index=0;
for(std::string::size_type i=0; i < s.length(); i++)
{
sum += s[i];
}
index = sum % MAX_LEN;
return index;
}
void insert(std::string s)
{
// Compute the index using Hash Function
int index = hashFunc(s);
// Insert the element in the linked list at the particular index
hashTable[index].push_back(s);
}
void search(string s)
{
//Compute the index by using the hash function
int index = hashFunc(s);
//Search the linked list at that specific index
for(int i = 0;i < hashTable[index].size();i++)
{
if(hashTable[index][i] == s)
{
cout << s << " is found!" << endl;
return;
}
}
cout << s << " is not found!" << endl;
}
int main(){

//opening text file
std::ifstream inFile;
inFile.open("un.txt");
// If text file doesnot exist or not included in root folder.
if(inFile.fail()) {
std::cerr << "Error opening file"<< std::endl ;
exit(1);
}
//if text file exists.
std::string wordsinfile;
std::string words[100];
int count=0,i=0;
std::string str;

// writing words from text file into Array.
while( !inFile.eof()) {
inFile >> wordsinfile;
words[i]=wordsinfile;
count++;
i++;
}
for (i=0;i<100;i++){
std::cout<< words[i]<<std::endl;
}

for(i=0;i<=23;i++) {
insert(words[i]);
}

int choice;
string z;
string y;
while(1) {
cout << "Enter choice. 1) Insertn 2) Searchn 3) Exitn";
cin >> choice;
switch (choice) {
case 1:
cin>>y;
insert(y);
break;
case 2:
cin>>z;
search(z);
break;
case 3:
exit(0);
}
}
return 0;
}

我的txt文件有38个不同的单词,哈希表的大小是27

以下是程序的正确版本。您的哈希表应该是字符串的集合,由于您在搜索函数中使用索引,因此使用STL向量作为哈希表应该可以做到这一点。

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <vector>
std::vector<std::string> hashTable[27];
int hashTableSize = 27;
#define MAX_LEN 27
using namespace std;

int hashFunc(std::string s)
{
// A simple hashing, no collision handled
int sum=0,index=0;
for(std::string::size_type i=0; i < s.length(); i++)
{
sum += s[i];
}
index = sum % MAX_LEN;
return index;
}
void insert(std::string s)
{
// Compute the index using Hash Function
int index = hashFunc(s);
// Insert the element in the linked list at the particular index
hashTable[index].push_back(s);
}
void search(string s)
{
//Compute the index by using the hash function
int index = hashFunc(s);
//Search the linked list at that specific index
for(int i = 0;i < hashTable[index].size();i++)
{
if(hashTable[index][i] == s)
{
cout << s << " is found!" << endl;
return;
}
}
cout << s << " is not found!" << endl;
}
int main(){
//opening text file
std::ifstream inFile;
inFile.open("un.txt");
// If text file doesnot exist or not included in root folder.
if(inFile.fail()) {
std::cerr << "Error opening file"<< std::endl ;
exit(1);
}
//if text file exists.
std::string wordsinfile;
std::vector<std::string> words;
std::string str;

// writing words from text file into Array.
while( inFile >> wordsinfile) {
words.push_back(std::move(wordsinfile));
}
std::cout << "Total words read: " << words.size() << std::endl;
for (int i = 0;i < words.size(); i++){
std::cout << words[i] << std::endl;
}

for(int i=0; i < words.size(); i++) {
insert(words[i]);
}

int choice;
string z;
string y;
while(1) {
cout << "Enter choice. 1) Insertn 2) Searchn 3) Exitn";
cin >> choice;
switch (choice) {
case 1:
cin>>y;
insert(y);
break;
case 2:
cin>>z;
search(z);
break;
case 3:
exit(0);
}
}
return 0;
}