Search code examples
c++dynamic-memory-allocationstdstring

Make dynamically allocated object type string


I need to make dynamically allocated object type of string to store sentences and after that sentences should be sorted in alphabetical order using std::sort.

This would be correct solution using char array:

#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;
}

When I try to convert this to dynamically allocated array type of string, like this:

#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;
}

I get a bunch of errors. Could you explain me how to convert this program of dynamically allocation of char array to dynamically allocated string array on a correct way?


Solution

  • It looks like you think std::string "corresponds to" char, but it corresponds to char*.
    You want std::string* sentence = nullptr;.
    (A lot of the point of this exercise is to notice how much easier it gets when you don't need to allocate the strings yourself.)

    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!";
      }
    }