创建字符串 C++ 的动态数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8948653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:22:45  来源:igfitidea点击:

creating dynamic array of string c++

c++arraysdynamic

提问by Abdul Samad

I am struct to a very basic question. I want to create dynamically an array of string in c++.

我是一个非常基本的问题的结构。我想在 C++ 中动态创建一个字符串数组。

How can I do that ?

我怎样才能做到这一点 ?

This is my attempt:

这是我的尝试:

#include <iostream>
#include <string>
int main(){
    unsigned int wordsCollection = 6;
    unsigned int length = 6;

    std::string *collection = new std::string[wordsCollection];
    for(unsigned int i = 0; i < wordsCollection; ++i){
        std::cin>>wordsCollection[i];
    }
    return 0;    
}

But it giving the following error

但它给出了以下错误

error C2109: subscript requires array or pointer type

What's the error ?

有什么错误?

And also if I'am getting input number from user, from std::cincan I create an array of that size statically ?

而且,如果std::cin我从用户那里获取输入数字,我可以静态创建一个该大小的数组吗?

回答by rerun

use std::vector<string>or std::list<string>over hand rolling it.

使用std::vector<string>std::list<string>手动滚动它。

回答by Johnsyweb

You meant to type:

你想输入:

std::cin>>collection[i];

And you also need to delete[]collection(or you'll leak this memory).

而且您还需要delete[]collection(否则您将泄漏此内存)。

It would be better use std::vector<std::string> collection;and avoid the raw pointer usage altogether:

最好使用std::vector<std::string> collection;并完全避免使用原始指针:

#include <iterator>
#include <iostream>
#include <string>
#include <vector>

int main()
{
    const unsigned int wordsCollection = 6;

    std::vector<std::string> collection;
    std::string word;
    for (unsigned int i = 0; i < wordsCollection; ++i)
    {
        std::cin >> word;
        collection.push_back(word);
    }

    std::copy(collection.begin(),
              collection.end(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
}

回答by steve8918

I think that should be:

我认为应该是:

std::cin >> collection[i];

回答by conectionist

You're getting this error because you're trying access the elements of an int (i.e. wordsCollection), not an array of int (i.e. collection). What you should be writing is

您收到此错误是因为您正在尝试访问 int (即wordsCollection)的元素,而不是 int 数组(即collection)。你应该写的是

std::cin>>collection[i]

回答by Mark Ransom

I think it's a simple typo. std::cin>>wordsCollection[i]should be std::cin>>collection[i].

我认为这是一个简单的错字。std::cin>>wordsCollection[i]应该是std::cin>>collection[i]

回答by thpatel

Try the following:

请尝试以下操作:

#include <vector>
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
    std::vector<std::string> myStrings;
    myStrings.push_back(std::string("string1"));
    myStrings.push_back(std::string("string2"));

    std::vector<std::string>::iterator iter = myStrings.begin();
    std::vector<std::string>::iterator end = myStrings.end();
    while(iter != end)
    {
        std::cout << (*iter) << std::endl;
        ++iter;
    }
    return 0;
}