【问题标题】:C++ Matrix to dynamic 2D arrray (with strings)C++ 矩阵到动态二维数组(带字符串)
【发布时间】:2016-05-01 16:19:14
【问题描述】:

假设我们有一个包含如下数据的 .txt 文件:

6
Paris New_York 1
London Berlin 1
Moskow Kiev 1
Paris London 1
New_York Moscow 1

其中 6 是城市数量,这意味着 Paris 和 New_York 与值 1 相连,它将始终为 1。

现在我想把它变成二维动态数组。我用这样的数字做到了,但我不知道我应该如何用字符串做到这一点。 对于数字:

ifstream myfile("info.txt");
if (myfile.is_open()) {
    getline(myfile, line);
    istringstream(line) >> Number; 
}

int **matrix= new int*[Number];
for (int i = 0; i < Number; i++) {
    matrix[i] = new int[Number];
}

while (getline(myfile, line)) {
    cout << line << '\n';
    std::stringstream linestream(line);
    int row;
    int column;
    int value;
    if (linestream >> row >> column >> value)
    {
        a[row-1][column-1] = value;
        a[column-1][row-1] = value;// mirror
    }

那么我怎样才能为字符串做到这一点呢? 感谢您提供有用的答案

【问题讨论】:

  • 老兄,Moscow 是用 w 写的,而不是 v
  • 你确定要转成数组吗?这就是现在每个人都会这样做的方式。有些人会用字典来做。如果你想用数组来做,你需要想出一种方法来将城市分配给数字。
  • 有人告诉我,它可以用“地图”来完成,但我不知道那是什么。我尝试自己做,但失败了。我在想,每次我们“读取”一个城市时,我们都需要遍历数组中的所有城市,看看它是否已经存在,然后将其存储在那里。
  • @C-PROG 您需要的功能在std::mapstd::unordered_map 中实现,因此您不必重新发明轮子。 map 使用平衡树来存储数据以进行快速查找(对数),而 unordered_map 使用哈希表来实现此目的。如果顺序很重要,您应该使用map,否则使用unordered_map,它具有(摊销)恒定的查找复杂性。我在下面使用unordered_map提供了一个解决方案。

标签: c++ string matrix dynamic-arrays


【解决方案1】:

您需要一个unordered_map&lt;string, int&gt;,在矩阵旁边,将字符串映射到索引。这是我的解决方案:

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <unordered_map>
using namespace std;

int main() {
  string line;
  int Number;

  ifstream myfile("info.txt");
  if (myfile.is_open()) {
    getline(myfile, line);
    istringstream(line) >> Number;
  }

  int **matrix= new int*[Number];
  for (int i = 0; i < Number; i++) {
    matrix[i] = new int[Number](); // note () at the end for initialization to 0
  }

  unordered_map<string, int> citiesMap; // to map cities (string) to indexes (int)
  int cityIndex = 0;

  while (getline(myfile, line)){
    std::stringstream linestream(line);
    string row;
    string column;
    int value;

    if (linestream >> row >> column >> value) {
      if(citiesMap.find(row) == citiesMap.cend())
        citiesMap[row] = cityIndex++; // add city to the map if it doesn't exist

      if(citiesMap.find(column) == citiesMap.cend())
        citiesMap[column] = cityIndex++; // add city to the map if it doesn't exist

      matrix[citiesMap[row]][citiesMap[column]] = value;
      matrix[citiesMap[column]][citiesMap[row]] = value;// mirror
    }
  }

  for(auto x: citiesMap) {
    cout << x.first << ": " << x.second << endl;
  }
  cout << endl;

  for(int i = 0; i < Number; i++) {
    for(int j = 0; j < Number; j++) {
      cout << matrix[i][j] << " ";
    }
    cout << endl;
  }
  // matrix should be freed here
}

您可以选择将唯一城市保留在向量(或数组)中,以从其索引中访问城市。不要忘记释放内存。此外,您可以将 std::array 用于矩阵,而不必担心内存问题。

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2023-03-06
    • 2023-03-28
    • 1970-01-01
    • 2014-11-24
    相关资源
    最近更新 更多