使动态分配的对象类型字符串

Make dynamically allocated object type string

我需要动态分配对象类型的字符串来存储句子,然后句子应该使用 std::sort 按字母顺序排序。

这将是使用 char 数组的正确解决方案:

#include <cstring>
#include <iostream>
#include <new>
#include <string>
#include <algorithm>
int main() {
  std::cout << "How many senteces: ";
  int n;
  std::cin >> n;
  std::cin.ignore(1000, '\n');
  char ** sentence = nullptr;
  std::cout << "Enter senteces:" << std::endl;
  try {
    sentence = new char * [n];
    for (int i = 0; i < n; i++)
      sentence[i] = nullptr;
    for (int i = 0; i < n; i++) {
      char temp[1000];
      std::cin.getline(temp, 1000);
      sentence[i] = new char[strlen(temp) + 1];
      strcpy(sentence[i], temp);
    }
    std::sort(sentence, sentence + n, [](const char * a,
      const char * b) {
      return std::strcmp(a, b) < 0;
    });
    std::cout << "Sorted sentences:" << std::endl;
    for (int i = 0; i < n; i++)
      std::cout << sentence[i] << std::endl;
    for (int i = 0; i < n; i++)
      delete[] sentence[i];
    delete[] sentence;
  } catch (...) {
    std::cout << "Problems with memory!";
    return 0;
  }
  return 0;
}

当我尝试将其转换为动态分配的字符串数组类型时,如下所示:

#include <cstring>
#include <iostream>
#include <new>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
  std::cout << "How many senteces: ";
  int n;
  std::cin >> n;
  std::cin.ignore(1000, '\n');
  std::string ** sentence = nullptr;
  std::cout << "Enter senteces:" << std::endl;
  try {
    sentence = new std::string * [n];
    for (int i = 0; i < n; i++)
      sentence[i] = nullptr;
    for (int i = 0; i < n; i++) {
      std::string temp;
      std::getline(std::cin, temp);
      sentence[i] = new std::string[temp.length() + 1];
      temp = sentence[i];
    }
    std::sort(sentence, sentence + n, [](std::string a,
      std::string b) {
      for (char & c: a) c = std::toupper(c);
      for (char & c: b) c = std::toupper(c);
      return a < b;
    });
    std::cout << "Sorted sentences:" << std::endl;
    for (int i = 0; i < n; i++)
      std::cout << sentence[i] << std::endl;
    for (int i = 0; i < n; i++)
      delete[] sentence[i];
    delete[] sentence;
  } catch (...) {
    std::cout << "Problems with memory!";
    return 0;
  }
  return 0;
}

我遇到了一堆错误。你能解释一下如何正确地将动态分配字符数组的程序转换为动态分配字符串数组吗?

看起来你认为std::string“对应于”char,但它对应于char*
你想要 std::string* sentence = nullptr;.
(这个练习的很多要点是注意当你不需要自己分配字符串时它会变得多么容易。)

int main() {
  try {
    std::cout << "How many sentences: ";
    int n;
    std::cin >> n;
    std::cin.ignore(1000, '\n');
    std::cout << "Enter sentences:" << std::endl;
    std::string* sentence = new std::string [n];
    for (int i = 0; i < n; i++) {
      std::getline(std::cin, sentence[i]);
    }
    std::sort(sentence, sentence + n, [](std::string a,
      std::string b) {
      for (char & c: a) c = std::toupper(c);
      for (char & c: b) c = std::toupper(c);
      return a < b;
    });
    std::cout << "Sorted sentences:" << std::endl;
    for (int i = 0; i < n; i++)
      std::cout << sentence[i] << std::endl;
    delete[] sentence;
  } catch (...) {
    std::cout << "Problems with memory!";
  }
}