阅读字典并打印出给定长度的单词 我试图把我用C写的旧程序转换成C++。其中一个部分需要读取字典文件,并将每个单词放入只包含该长度单词的向量中。所有较小的向量将被放入一个较大的外部向量中。在这个基本的例子中,我试着读入几个单词,然后打印出给定大小的单词。当我尝试测试它时,即使文件中存在这样大小的单词,也不会打印出任何单词。这个代码有什么问题 #include <iostream> #include <fstream> #include <vector> #include <stdlib.h> using namespace std; vector< vector<string> >readDictionary(void) { vector< vector<string> > outer; vector<string>::iterator iterator; int letters; ifstream dict; for(int i = 0; i < 29; i++) { vector<string> inner; outer.push_back(inner); } dict.open("dictionary.txt"); if(!dict.is_open()) { cout << "Error opening the dictionary. Exiting" << endl; exit(1); } while(!dict.eof()) { string word; getline(dict,word); if(word.size() > 0) { vector<string> inner = outer.at(word.size() - 1); inner.push_back(word); } } cout << "Letters: "; cin >> letters; vector<string> inner = outer.at(letters - 1); for(iterator = inner.begin(); iterator != inner.end(); iterator++) { cout << *iterator << endl; } return outer; } #包括 #包括 #包括 #包括 使用名称空间std; vectorreadDictionary(无效) { 向量外部; 向量::迭代器迭代器; 整数字母; ifstream-dict; 对于(int i=0;i

阅读字典并打印出给定长度的单词 我试图把我用C写的旧程序转换成C++。其中一个部分需要读取字典文件,并将每个单词放入只包含该长度单词的向量中。所有较小的向量将被放入一个较大的外部向量中。在这个基本的例子中,我试着读入几个单词,然后打印出给定大小的单词。当我尝试测试它时,即使文件中存在这样大小的单词,也不会打印出任何单词。这个代码有什么问题 #include <iostream> #include <fstream> #include <vector> #include <stdlib.h> using namespace std; vector< vector<string> >readDictionary(void) { vector< vector<string> > outer; vector<string>::iterator iterator; int letters; ifstream dict; for(int i = 0; i < 29; i++) { vector<string> inner; outer.push_back(inner); } dict.open("dictionary.txt"); if(!dict.is_open()) { cout << "Error opening the dictionary. Exiting" << endl; exit(1); } while(!dict.eof()) { string word; getline(dict,word); if(word.size() > 0) { vector<string> inner = outer.at(word.size() - 1); inner.push_back(word); } } cout << "Letters: "; cin >> letters; vector<string> inner = outer.at(letters - 1); for(iterator = inner.begin(); iterator != inner.end(); iterator++) { cout << *iterator << endl; } return outer; } #包括 #包括 #包括 #包括 使用名称空间std; vectorreadDictionary(无效) { 向量外部; 向量::迭代器迭代器; 整数字母; ifstream-dict; 对于(int i=0;i,c++,file,dictionary,C++,File,Dictionary,简单的打字错误。您忘记在这一行中使用引用: vector<string> inner = outer.at(word.size() - 1); vector-inner=outer.at(word.size()-1); 代码按原样复制向量,添加向量,然后销毁它。简单的输入错误。您忘记在这一行中使用引用: vector<string> inner = outer.at(word.size() - 1); vector-i

简单的打字错误。您忘记在这一行中使用引用:

            vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);

代码按原样复制向量,添加向量,然后销毁它。

简单的输入错误。您忘记在这一行中使用引用:

            vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);

代码按原样复制向量,添加向量,然后销毁它。

简单的输入错误。您忘记在这一行中使用引用:

            vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);

代码按原样复制向量,添加向量,然后销毁它。

简单的输入错误。您忘记在这一行中使用引用:

            vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);

在代码保持原样的情况下,它复制向量,添加到向量中,然后销毁它。

主要问题是
vector::at()
返回对它存储的任何类型的对象的引用。但是您没有在'if'语句中使用引用。您的行:

vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);
正在创建所需向量的副本,然后该副本在超出范围时被销毁

尝试将其替换为:

vector<string>& inner = outer.at(word.size() - 1);
vector&inner=outer.at(word.size()-1);

对“for”循环正上方的行执行相同的操作。

主要问题是
vector::at()
返回对它存储的任何类型对象的引用。但您没有在“if”语句中使用引用。您的行:

vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);
正在创建所需向量的副本,然后该副本在超出范围时被销毁

尝试将其替换为:

vector<string>& inner = outer.at(word.size() - 1);
vector&inner=outer.at(word.size()-1);

对“for”循环正上方的行执行相同的操作。

主要问题是
vector::at()
返回对它存储的任何类型对象的引用。但您没有在“if”语句中使用引用。您的行:

vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);
正在创建所需向量的副本,然后该副本在超出范围时被销毁

尝试将其替换为:

vector<string>& inner = outer.at(word.size() - 1);
vector&inner=outer.at(word.size()-1);

对“for”循环正上方的行执行相同的操作。

主要问题是
vector::at()
返回对它存储的任何类型对象的引用。但您没有在“if”语句中使用引用。您的行:

vector<string> inner = outer.at(word.size() - 1);
vector-inner=outer.at(word.size()-1);
正在创建所需向量的副本,然后该副本在超出范围时被销毁

尝试将其替换为:

vector<string>& inner = outer.at(word.size() - 1);
vector&inner=outer.at(word.size()-1);

并对“for”循环上方的行执行相同的操作。

只是一个建议,您可以使用带有关键字的
映射
,以及
字符串
s的
向量
,请参见下面的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>

std::map<int, std::vector<std::string>> readDictionary(char const *filename)
{
  std::map<int, std::vector<std::string>> out;
  std::ifstream dict(filename);
  if (!dict.is_open()) throw std::runtime_error("Error opening the dictionary.");
  while (!dict.eof()) {
    std::string word;
    std::getline(dict, word, ' ');
    if (!word.empty()) out[word.size()].push_back(word);
  }

  return out;
}

int main()
{
  auto v = readDictionary("dictionary.txt");
  for (auto i : v) {
    std::cout << "Length " << i.first << ":" << std::endl;
    for (auto j : i.second) std::cout << "    " << j << std::endl;
  }
  return 0;
}
#包括
#包括
#包括
#包括
#包括
std::map readDictionary(字符常量*文件名)
{
标准::制定计划;
std::ifstream dict(文件名);
如果(!dict.is_open())抛出std::runtime_错误(“打开字典时出错”);
而(!dict.eof()){
字符串字;
std::getline(dict,word,”);
如果(!word.empty())输出[word.size()],则向后推(word);
}
返回;
}
int main()
{
auto v=readDictionary(“dictionary.txt”);
用于(自动i:v){

std::cout只是一个建议,您可以使用一个
映射
,其中包含单词的大小和
字符串的
向量
,请参见下面的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>

std::map<int, std::vector<std::string>> readDictionary(char const *filename)
{
  std::map<int, std::vector<std::string>> out;
  std::ifstream dict(filename);
  if (!dict.is_open()) throw std::runtime_error("Error opening the dictionary.");
  while (!dict.eof()) {
    std::string word;
    std::getline(dict, word, ' ');
    if (!word.empty()) out[word.size()].push_back(word);
  }

  return out;
}

int main()
{
  auto v = readDictionary("dictionary.txt");
  for (auto i : v) {
    std::cout << "Length " << i.first << ":" << std::endl;
    for (auto j : i.second) std::cout << "    " << j << std::endl;
  }
  return 0;
}
#包括
#包括
#包括
#包括
#包括
std::map readDictionary(字符常量*文件名)
{
标准::制定计划;
std::ifstream dict(文件名);
如果(!dict.is_open())抛出std::runtime_错误(“打开字典时出错”);
而(!dict.eof()){
字符串字;
std::getline(dict,word,”);
如果(!word.empty())输出[word.size()],则向后推(word);
}
返回;
}
int main()
{
auto v=readDictionary(“dictionary.txt”);
用于(自动i:v){

std::cout只是一个建议,您可以使用一个
映射
,其中包含单词的大小和
字符串的
向量
,请参见下面的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>

std::map<int, std::vector<std::string>> readDictionary(char const *filename)
{
  std::map<int, std::vector<std::string>> out;
  std::ifstream dict(filename);
  if (!dict.is_open()) throw std::runtime_error("Error opening the dictionary.");
  while (!dict.eof()) {
    std::string word;
    std::getline(dict, word, ' ');
    if (!word.empty()) out[word.size()].push_back(word);
  }

  return out;
}

int main()
{
  auto v = readDictionary("dictionary.txt");
  for (auto i : v) {
    std::cout << "Length " << i.first << ":" << std::endl;
    for (auto j : i.second) std::cout << "    " << j << std::endl;
  }
  return 0;
}
#包括
#包括
#包括
#包括
#包括
std::map readDictionary(字符常量*文件名)
{
标准::制定计划;
std::ifstream dict(文件名);
如果(!dict.is_open())抛出std::runtime_错误(“打开字典时出错”);
而(!dict.eof()){
字符串字;
std::getline(dict,word,”);
如果(!word.empty())输出[word.size()],则向后推(word);
}
返回;
}
int main()
{
auto v=readDictionary(“dictionary.txt”);
用于(自动i:v){

std::cout只是一个建议,您可以使用一个
映射
,其中包含单词的大小和向量的值a
vector