简体   繁体   English

我不明白为什么我对字符串的排序会破坏一切

[英]I don't understand why my sort on a string breaks everything

I have the following code:我有以下代码:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

vector<vector<string>> findAnagrams(vector<string> wordlist) {
  vector<vector<string>> result;
  unordered_map<string, vector<string>*> indexes;

  for (const string& word : wordlist) {
    string wordSorted = word;
    sort(wordSorted.begin(), wordSorted.end()); // <= This line break everything

    auto index = indexes.find(wordSorted);
    if (index == indexes.end()) {
      vector<string> vec = { word };
      result.push_back(vec);
      indexes[wordSorted] = &vec;
    } else {
      index->second->push_back(word);
    }
  }

  return result;
}

int main()
{
    vector<string> wordlist = {"eat", "tea", "tan", "ate", "nat", "bat", "test", "estt"};
    auto result = findAnagrams(wordlist);

    for (const auto& vec : result) {
      for (const auto& word : vec) {
        cout << word << " ";
      }
      cout << endl;
    }

    return 0;
}

This code detects all anagrams in a list of given words.此代码检测给定单词列表中的所有字谜。

As my comment says, when I sort wordSorted using std::sort , it breaks everything and my code ends with a bad_alloc.正如我的评论所说,当我使用std::sortwordSorted进行排序时,它会破坏所有内容,并且我的代码以 bad_alloc 结尾。 As if the std::sort manipulates the memory outside of wordSorted .好像std::sort在 wordSorted 之外操纵wordSorted If I remove this specific line, the code "works" (the result is obviously wrong, but it does what it should do).如果我删除这个特定的行,代码“工作”(结果显然是错误的,但它做了它应该做的)。

How it is possible?怎么可能? What am I missing?我错过了什么?

I'm guessing these lines are the main cause of your problem:我猜这些行是你的问题的主要原因:

{
    vector<string> vec = { word };
    result.push_back(vec);
    indexes[wordSorted] = &vec;
}

Here you store a pointer to the local variable vec in the indexes map.在这里,您将指向局部变量vec的指针存储在indexes map 中。 When the block ends at } the life-time of vec also ends, and the pointer you just stored will become invalid.当块在}结束时, vec的生命周期也结束了,你刚刚存储的指针将变得无效。

Any use of this pointer will lead to undefined behavior .对该指针的任何使用都会导致未定义的行为

It seems to me that the solution is to simply not store pointers to the vector (pointers to containers are seldom, if ever, needed), and instead store a copy.在我看来,解决方案是简单地不存储指向向量的指针(指向容器的指针很少需要,如果有的话),而是存储一个副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 2 个字符串的字谜:我不明白我的代码有什么问题 - Anagram of 2 string : I don't understand what is the problem with my code 修复了我的内存泄漏,我不明白为什么会起作用 - Fixed my memory leak and I don't understand why it worked 我不明白为什么这会导致我的程序崩溃? - I don't understand why this is causeing my program to crash? 我不明白为什么我的代码不起作用并且需要更长的时间才能运行 - I don't understand why my code doesn't work and take longer to run 我试图在我的C ++类中声明一个常量字符串,但是我得到了“无效的类内任务”,我不明白为什么 - I'm trying to declare a constant string in my C++ class, but I get an “Invalid in-class assignment”, and I don't understand why 在我的代码中使用 htons() 将所有零放在缓冲区中,我不明白为什么 - Using htons() in my code puts all zeros in the buffer and I don't understand why 我不明白为什么我的c ++代码运行得这么慢 - I don't understand why my c++ code running so slow 我不明白为什么我的二进制搜索代码显示分段错误 - I don't understand why my code for binary search displays a segmentation error Sqlite3,我不明白为什么我的指针返回为空 - Sqlite3, I don't understand why my pointer is returned nulled 我不明白为什么这个功能不能正常工作 - I don't understand why this function isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM