C++ C++;根据用户输入声明内容和大小的字符串数组
我试图设置一个字符串数组,其大小和内容取决于用户输入。我在声明数组时出错,它说大小的变量类型不正确。我在这上面花了几个小时,只是想问一下 这是我的密码:C++ C++;根据用户输入声明内容和大小的字符串数组,c++,xcode,dynamic,C++,Xcode,Dynamic,我试图设置一个字符串数组,其大小和内容取决于用户输入。我在声明数组时出错,它说大小的变量类型不正确。我在这上面花了几个小时,只是想问一下 这是我的密码: #include <iostream> #include <string> using namespace std; int main() { cout << "Enter number of names /n"; int a; cin >> a; string
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter number of names /n";
int a;
cin >> a;
string namesArray[a]; //Error is here.
for( int i=0; i<a; i++) {
string temp;
cin >> temp;
namesArray[i] = temp;
}
for( int j=0; j<a; j++) {
cout << "hello " << namesArray[j] << "/n";
}
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
cout>a;
字符串名称数组[a];//此处有错误。
对于(int i=0;i>temp;
名称数组[i]=temp;
}
对于(int j=0;j数组的大小需要有一个编译时值。您的代码不会编译,因为a
不是编译时常量
更好地使用std::vector
:
#include <iostream>
#include <string>
#include <vector> // <-- Must be included
using namespace std;
int main()
{
cout << "Enter number of names /n";
int a;
cin >> a;
vector<string> namesArray; // HERE!!!
for( int i=0; i < a; i++) {
string temp;
cin >> temp;
namesArray.push_back(temp); // Notice the difference
}
for( int j=0; j<a; j++) {
cout << "hello " << namesArray[j] << "/n";
}
return 0;
}
#包括
#包括
#包括//a;
向量名称数组;//这里!!!
for(int i=0;i>温度;
namesArray.push_back(temp);//注意区别
}
对于(int j=0;j数组的大小需要有一个编译时值。您的代码不会编译,因为a
不是编译时常量
更好地使用std::vector
:
#include <iostream>
#include <string>
#include <vector> // <-- Must be included
using namespace std;
int main()
{
cout << "Enter number of names /n";
int a;
cin >> a;
vector<string> namesArray; // HERE!!!
for( int i=0; i < a; i++) {
string temp;
cin >> temp;
namesArray.push_back(temp); // Notice the difference
}
for( int j=0; j<a; j++) {
cout << "hello " << namesArray[j] << "/n";
}
return 0;
}
#包括
#包括
#包括//a;
向量名称数组;//这里!!!
for(int i=0;i>温度;
namesArray.push_back(temp);//注意区别
}
对于(int j=0;j,可以通过以下方式声明名称数组:
string * namesArray = new string[a];
这应该是可行的,因为它根据输入值a动态分配内存
但当然,最好使用vector。如果使用vector,则不需要删除数组。您可以通过以下方式声明名称数组:
string * namesArray = new string[a];
这应该是可行的,因为它根据输入值a动态分配内存
但当然,最好使用vector。如果使用vector,则不需要删除数组。不能使用变量作为静态初始化数组的大小,为此,需要动态分配数组,如
string* namesArray = new string[a];
但是使用std::vector来避免内存泄漏要明智得多
使用向量,您可以这样做:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "Enter number of names /n";
int a;
cin >> a;
vector<string> names;
for( int i=0; i<a; i++) {
string temp;
cin >> temp;
names.push_back(temp);
}
vector<string>::iterator
it = names.begin(),
ite = names.end();
for(; it != ite; ++it) {
cout << "hello " << *it << "/n";
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout>a;
载体名称;
对于(int i=0;i>temp;
名称。推回(临时);
}
向量:迭代器
it=names.begin(),
ite=names.end();
for(;it!=ite;++it){
cout您不能使用变量作为静态初始化数组的大小,为此,您需要动态分配数组,例如
string* namesArray = new string[a];
但是使用std::vector来避免内存泄漏要明智得多
使用向量,您可以这样做:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "Enter number of names /n";
int a;
cin >> a;
vector<string> names;
for( int i=0; i<a; i++) {
string temp;
cin >> temp;
names.push_back(temp);
}
vector<string>::iterator
it = names.begin(),
ite = names.end();
for(; it != ite; ++it) {
cout << "hello " << *it << "/n";
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout>a;
载体名称;
对于(int i=0;i>temp;
名称。推回(临时);
}
向量:迭代器
it=names.begin(),
ite=names.end();
for(;it!=ite;++it){
cout正如Mark所说,这是一个编译时问题。您可以使用vector
,但另一种方法是为数组动态分配内存。这意味着使用关键字new
因此,您的代码应该是string*namesArray=newstring[a];
使用new
返回指向数组的指针,因此进行相应调整。正如Mark所说,这是编译时问题。您可以使用vector
,但另一种方法是为数组动态分配内存。这意味着使用关键字new
因此,您的代码应该是string*namesArray=newstring[a];
使用new
返回指向数组的指针,因此进行相应调整。变量不能用作数组大小,您必须使用编译时常量。您需要的是std::vector
,一个运行时可调整大小的数组类。请发布您得到的确切错误。变量不能用作数组大小,您必须使用编译-时间常数。您需要的是std::vector
,这是一个可在运行时调整大小的数组类。请发布您得到的确切错误。但是,您需要小心确保它不会超出范围以防止内存泄漏。虽然它位于main中,但这不应该是一个问题。但是,您需要小心确保它永远不会出现不在防止内存泄漏的范围内。但由于它位于main中,这不应该是问题。我如何使用dow?新建导入?@user1807880刚刚编辑。我如何使用dow?新建导入?@user1807880刚刚编辑。