#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
static int c;
class DAI //DynamicArrayInput
{
public :
void getCountry()
{
cout << "Country : ";
cin >> Country;
}
void putCountry()
{
cout << Country << endl;
}
static int putCount()
{
return count;
}
private :
static int count;
char Country[30];
};
int main()
{
vector<DAI> A;
DAI B[3];
//DAI * B;
cout << "Enter name of countries\n";
for(int i=0; i < 3; i++)
{
//THIS DOESN'T WORK
/*
c++; //c is a static variable declared at top
B = new DAI[sizeof(DAI) * c/sizeof(DAI)];
GIVES WRONG OUTPUT :
Enter name of countries
Country : a
Country : b
Country : c
Printing name of countries
c
size of vector A is 3
vector after initialisation:
*/
A.push_back(B[i]);
B[i].getCountry();
}
cout << "Printing name of countries\n";
for(int i=0; i < 3; i++)
{
B[i].putCountry();
}
cout << "size of vector A is " << A.size() << "\
\nvector after initialisation:" << endl;
return 0;
}
**/*
CORRECT OUTPUT WITH LIMITED ARRAY SIZE = 3:
*Enter name of countries
Country : a
Country : b
Country : c
Printing name of countries
a
b
c
size of vector A is 3
vector after initialisation:*
*/**
我刚学,这是不是我的功课, 我不提到它希望被局限于任何数组的大小,如上述,但是当我尝试使用staements在第一个for循环不行 我的意思是,这并不适用动态添加对象?
c++; //c is a static variable declared at top
B = new DAI[sizeof(DAI) * c/sizeof(DAI)]; //B is a pointer of type class DAI
我的目标是使用对象我喜欢动态数组,这样我可以输入任意数量的国家,我不希望使用类似B [3]已知的数组大小,但我无法做到。
请指导我,帮帮我。
使用一个'std :: vector'或'std :: deque',它们是自动调整大小的容器。我看到你正在向量中存放数组 - 你可能已经有了'std :: vector>'。 –
birryree
2011-04-27 15:27:36
嗯,我已经在程序中使用它,但无法继续它看到主要的第一行 – mukesh 2011-04-27 15:33:45
你不会初始化c任何地方。 – 2011-04-27 15:37:10