在c++中将字符串转换为字符
Conversion of string to char in c++
我正在尝试使用哈希表构建一个拼写检查验证器。我有一个文本文件中的单词列表。我想将它们导入到程序中,并使用单独的链接将它们输入到哈希表中。现在,我想运行程序,但我有这两个错误。有人能帮我吗?
第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;
}
相关文章:
- HEX值到wchar_t字符(UTF-8)的转换
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 转换特殊字符(UTF-8)
- 将转换字符键入 int 以用作向量C++的索引
- 将字符随机转换为大写的函数
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 转换字符时Arduino DUE 和 Arduino UNO 之间的区别
- 如何在C++中正确重载运算符转换字符*?
- 如何转换数据结构以在 c++ 中转换字符*
- 不兼容的转换字符
- 将转换字符数组[16]键入为int 128位
- 在c++中转换字符编码
- 使用递归转换字符
- 如何在Objective-C/C/C++中转换字符和字节的位置
- C++到Pep/8汇编语言的转换——字符数组
- 转换字符>十六进制