-
newb question
Hi,
i have a question about inserting structvalues in a list.
Code:
#include <list>
#include <iostream>
typedef struct{
int x;
int y;
}tfingertips;
using namespace std;
typedef list<tfingertips> FINGERLIST;
int main ()
{
FINGERLIST::push_back() = ;
FINGERLIST::push_back() = ;
FINGERLIST::push_back() = ;
return 1;
}
i want to add now values to that list. How can i do it? i have multiple books but they do not describe how to *grmbl*
Thanks in advance!
~Jan
-
Try:
FINGERLIST.push_back(some_struct);
-
Code:
#include <list>
#include <iostream>
typedef struct{
int x;
int y;
}tfingertips;
using namespace std;
typedef list<tfingertips> FINGERLIST;
int main()
{
FINGERLIST fingerList;//Create instance
tfingertips ft = {1,1};
fingerList.push_back(ft);
}
-
now i can add stuff, but the reading seems not to work.
Code:
#include <list>
#include <iostream>
typedef struct{
int x;
int y;
}tfingertip;
using namespace std;
typedef list<tfingertip> FINGERLIST;
int main ()
{
FINGERLIST flist;
tfingertip tft;
for (int j = 0; j < 20; j++){
tfingertip tft = {j,j*2};
flist.push_back(tft);
};
for ( int i = 0; i < 5; i++){
cout << "x: " << tft.x << endl << "y: " << tft.y << endl;
cout << endl;
};
return 1;
}
its simple but my mind is blocked actually.
Thanks in advance!
~Jan
-
Code:
typedef FINGERLIST::const_iterator fiter;
for(fiter i = flist.begin();i != flist.end();++i)
cout << "x: " << i->x << " y: " << i->y << endl;
-
> i have a question about inserting structvalues in a list.
Then it would be nice if you'd title the thread something like "Structs and Lists" so that users can find this post in a search later... Not trying to be rude or anything, but I know I like to do searches first so I can avoid redundant posts! :)
Sorry I can't help you with the code, I'm not as familiar with the old-style typedef. But Wledge's code looks pretty nifty, I didn't realize you could do that sort of thing!