C++ 声明指向字符串的指针的动态数组时出现问题

C++ 声明指向字符串的指针的动态数组时出现问题,c++,string,C++,String,在执行在线练习作业时,我遇到了一个我无法解决的问题 用户必须输入一个数字(他将输入的句子数),然后继续逐个输入句子,这些句子将存储为字符串(顺便说一句,声明动态指针数组是必需的)。然而,由于句子的数量不是先验的,我知道指针数组的大小实际上是句子的数量,但我不知道如何声明指向字符串的指针的动态数组 使用一些我事先就知道的东西,我想出了如何使用字符数组而不是字符串数组来做同样的事情。声明指向动态字符数组的指针动态数组的行如下所示: char **ptr=new char*[n] {}; 根据我的理

在执行在线练习作业时,我遇到了一个我无法解决的问题

用户必须输入一个数字(他将输入的句子数),然后继续逐个输入句子,这些句子将存储为字符串(顺便说一句,声明动态指针数组是必需的)。然而,由于句子的数量不是先验的,我知道指针数组的大小实际上是句子的数量,但我不知道如何声明指向字符串的指针的动态数组

使用一些我事先就知道的东西,我想出了如何使用字符数组而不是字符串数组来做同样的事情。声明指向动态字符数组的指针动态数组的行如下所示:

char **ptr=new char*[n] {};
根据我的理解,这创建了一个指针ptr,它指向一个指针的动态数组,每个指针的元素指向一个字符数组。我现在想做一些类似的事情,结果应该是
ptr
是指向动态指针数组的指针,其中每个元素指向一个字符串


有人能帮忙吗?我会很感激的

您可以完全避免使用指针并使用

std::vector<std::string> input;
但是使用向量更容易。
每个
pInput
都将是一个字符串,就像
std::vector
版本一样。

我想你要找的是

std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::string *sentences = new std::string[num];
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/
delete[] sentences;     // free the memory
std::size\u t num;
std::cout>num;
std::string*句=新std::string[num];
对于(std::size\u t i=0;i!=num;++i){
std::cout num;
向量句{num};
对于(std::size\u t i=0;i!=num;++i){
std::cout num;
std::unique_ptr语句{new std::string[num]};
对于(std::size\u t i=0;i!=num;++i){

STD::你愿意使用<代码> STD::String ,或者你想在C++中用< Case> char < /代码>指针来做这个吗?是的,正如我提到的,我必须使用<代码> STD::String 。这是一个很好的方法。如果你需要更多的字符串,你必须编写一个realloc方案,基本上你可以声明一个指针的新闻集(双倍的数字),将旧的复制到新的,
delete[]old
,然后设置
old=newset
,然后继续。非常感谢,这似乎很有效。不过我还有一个问题(如果你有时间回答的话)。我想这可能是我自己想出来的,但让我困惑的是
std::string*句=new std::string[num];
是指向字符串的指针,但是
new std::string[num]
是我以前从未见过的东西。
std::string
在内部不是一个动态分配的数组吗?
new
与string一起做什么?再一次为我的无知感到抱歉,我真的是一个新手。感谢你的巨大帮助!@I0ner9如果这个答案有帮助,请投票并/或接受它这是标准的
operator new[]
语法,如
int*ptr=new int[42];
,只是您分配的不是
int
而是
std::string
。是的,内部
std::string
也可以使用动态分配来存储长字符串,但这在这里并不重要:
new
操作符只分配
std::string
实际需要的内存,而不是需要的额外内存然后可以分配字符串。
std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::string *sentences = new std::string[num];
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/
delete[] sentences;     // free the memory
std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::vector<std::string> sentences{num};
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/
std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::unique_ptr<std::string[]> sentences{new std::string[num]};
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/