我有一些理解指针数组的问题,请参阅以下简化代码
#include <string>
#include <iostream>
using namespace std;
//holds two different datatypes
template <typename A, typename B>
class data {
public:
A dataA;
B dataB;
data(){}
data* add(A dataA, B dataB){
this->dataA = dataA;
this->dataB = dataB;
return this;
}
};
//stores an array of the data
template <typename A, typename B>
class container {
data<A, B> **dataArray;
int ammount;
public:
typedef data<A, B> * iterator;
container(){
//create dynamic array of pointers
dataArray = new data<A, B>*[5];
ammount = 0;
}
~container(){
for (int i = 0; i <= ammount; i++)
{
delete dataArray[i];
}
}
void add(A dataA, B dataB){
data<A, B> *newItem;
if (ammount <= 5){
newItem = new data<A, B>();
dataArray[ammount] = newItem->add(dataA, dataB);
ammount++;
}
}
//basic iterators
iterator begin(){
return *dataArray;
}
iterator end(){
return dataArray[5];
}
};
void main(){
string somestuff[] = { "one", "two", "three", "four", "five" };
container<string, int> foo;
for (int i = 0; i < 5; i++){
foo.add(somestuff[i], i);
}
container<string, int>::iterator it = foo.begin();
// why cant i just iterate through each item in the array
for (it; it < foo.end(); it++)
{
cout << "Data A : " << it->dataA << " Data B : " << it->dataB << "\n";
}
}
问题是主要的,我不能在显示时移动数组。我看这个问题都错了吗?
ps我知道STL容器在这里会更好但是我需要理解这个特定的场景,即指向对象的动态数组(不使用向量)
**编辑 我添加了一些输出语句来显示我想要实现的目标
输出
Adding data
Added Data at address : 00779218
Added Data at address : 00779458
Added Data at address : 007794B8
Added Data at address : 00779560
Added Data at address : 00779730
Printing data
The address of 'it' is now: 00779218
Data A : one Data B : 0
incerementing from map.begin
The address of 'it' is now: 00779238
Data A : Data B : 469784212
incerementing from map.begin
The address of 'it' is now: 00779258
Data A : X Data B : -33686019
incerementing from map.begin
The address of 'it' is now: 00779278
当我这样做时
for (it; it < foo.end(); it++){
cout << "The address of 'it' is now: "<<it<<"\n";
cout << "Data A : " << it->dataA << " Data B : " << it->dataB << "\n";
cout << "incerementing from map.begin\n";
}
我在dataArray的下一个项目中需要'it'变量顶点,它正在递增而不正确
答案 0 :(得分:0)
试试这个程序:
#include <string>
#include <iostream>
using namespace std;
//holds two different datatypes
template <typename A, typename B>
class data {
public:
A dataA;
B dataB;
data* _next;
data(){_next = NULL;}
data* add(A dataA, B dataB){
this->dataA = dataA;
this->dataB = dataB;
return this;
}
bool operator<(data& rhs)
{
if (this->dataB ==rhs.dataB);
return true;
return (this->dataB <rhs.dataB);
}
data* operator++()
{
if(this == NULL)
return NULL;
return _next;
}
};
//stores an array of the data
template <typename A, typename B>
class container {
data<A, B> **dataArray;
int ammount;
public:
typedef data<A, B> * iterator;
container(){
//create dynamic array of pointers
dataArray = new data<A, B>*[5];
ammount = 0;
}
~container(){
for (int i = 0; i < ammount; i++)
{
delete dataArray[i];
}
}
void add(A dataA, B dataB){
data<A, B> *newItem;
if (ammount <= 5){
newItem = new data<A, B>();
dataArray[ammount] = newItem->add(dataA, dataB);
if(ammount != 0)
{
dataArray[ammount -1]->_next = dataArray[ammount];
}
ammount++;
}
}
//basic iterators
iterator begin(){
return *dataArray;
}
iterator end(){
return dataArray[4];
}
};
int main(){
string somestuff[] = { "one", "two", "three", "four", "five" };
container<string, int> foo;
for (int i = 0; i < 5; i++){
foo.add(somestuff[i], i);
}
container<string, int>::iterator it = foo.begin();
// why cant i just iterate through each item in the array
for (it; *it < *foo.end(); it =++ *it)
{
if(it != NULL)
cout << "Data A : " << it->dataA << " Data B : " << it->dataB << "\n";
else
return 0;
}
return 0;
}