Click here to Skip to main content
13,595,908 members
Rate this:
Please Sign up or sign in to vote.
Mine this structure is global an di added it into the stl

struct Fun
    int a;
    char* c;

i added it into the stl as follows
vector<FUN > SS;

Now how can i do the following
1) i access the elements of structure through the list pointer.
2) if i want to pass the list as an argument to the function what should be the declaration of the structure.
3)how can i get the nodes from the list one by one.
Posted 30-Oct-12 9:06am
Updated 30-Oct-12 11:03am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Well, here's one way you could go about it. I've passed the vector by reference (put the & char in front of the var name in the function sig) because we may be talking about megabytes of data. There's no need to copy and pass all of that data to the function - we can simply tell the function where to find it.

I could have done this with pointers * or references &
It's much of a muchness in this situation.

Though do be aware - by passing as a pointer or a reference, if you modify the input argument in the function, it will modify the actual data itself - not just a copy of it.

You can try this by clearing the values of each struct in method 1, after you've printed them. Notice now how method 2 shows the new values? If you then remove the & from the function signature and try again, notice there's no effect?

Also, shouldn't really be mixing C/C++ code - i.e FUN.c should probably be a string. As it stands, I don't de-allocate the memory gained with the calls to strdup - this will create a memory leak.. But that's another matter. I've aimed to keep the code understandable for you. :)

Anyway, enough ado about nothing. Here's the code:

#include <iostream>
#include <vector>
#include "string.h"
using namespace std;

struct Fun
    int a;
    char* c;

typedef vector<Fun> vecFun;
typedef vecFun::iterator vecFunIter;

void method1(vecFun inputList)
//void method1(vecFun &inputList)
    cout << "-------------------------------------------------------" << endl;
    cout << "method 1 for access - use the [] operator of the vector" << endl;
    cout << "-------------------------------------------------------" << endl;
    int i, n;
    n = inputList.size();
    for (i=0; i<n; i++)
        cout << "Activity:  " << inputList[i].c << ", Fun level: " << inputList[i].a << endl;
        // this will change the value shown by method 2 - so long as we passed inputList by refernce
        // or if we passed a pointer to it. - try to use the other function header and see what happens!
        inputList[i].a = 100; 

void method2(vecFun &inputList)
    cout << "-------------------------------------" << endl;
    cout << "method 2 for access - use an iterator"<<endl;
    cout << "-------------------------------------" << endl;
    vecFunIter myIter;
    for (myIter=inputList.begin(); myIter!=inputList.end(); myIter++)
        cout << "Activity:  " << myIter->c << ", Fun level: " << myIter->a << endl;

int main()
    Fun myTemp;
    vecFun funList;

    myTemp.a = -1;
    myTemp.c = strdup("Having teeth pulled");

    myTemp.a = 1;
    myTemp.c = strdup("Having haircut");

    myTemp.a = 4;
    myTemp.c = strdup("Coding");

    cout << endl;
    // EDIT:
    // free the memory we grabbed in the strdup calls.
    vecFunIter mIter;
    for (mIter=funList.begin(); mIter!=funList.end(); mIter++)
Eugen Podsypalnikov 30-Oct-12 15:52pm
3 stars - for three memory leaks :)
enhzflep 30-Oct-12 15:57pm
Pleasant reply - for the :) icon.

After all, I did mention the memory leaks. Got the inclination to write a better example? Perhaps you'd like to edit mine? :-)
Eugen Podsypalnikov 30-Oct-12 17:26pm
Yes, I have got inclination to write the structure destructor properly :)
Eugen Podsypalnikov 30-Oct-12 18:15pm
Very understandable solution :)
enhzflep 30-Oct-12 17:48pm
Works for me. The formatting's not my preference - but that's no reason to deny you a 5. I'll fix mine for the op and future readers.
Tarun Batra 31-Oct-12 7:04am
Sir can you please tell if some time in future i want to update status field in the structure that has been added into the list how can i do that?
Eugen Podsypalnikov 31-Oct-12 7:20am
In the current context (list of the structure-copies),
you have to iterate the list to find the needed structure firstly
(for example, it could be identified by a member inside the structure).
The searching process can be slowly ("magabytes of data") inside the list :)
That is why, you could also use a std::map<DWORD, MYSTRUCT*> as the data container
and then access a needed MYSTRUCT* by the using of std::map::operator[] :
void MyContext::EditById(DWORD dwId, int iNewValue)
MYSTRUCT* pMS(m_map[dwId]);
if (pMS) {
pMS->iData = iNewValue;
enhzflep 31-Oct-12 7:46am
Okay. Cool. As I said in my original response, making the variable a reference (by using the & character) you're telling the function where to find the original data. Since the function doesn't get a copy of the data, changing it in the function means your changing the list you created in main. You could just as easily change it in main, with: funList[index].a or funcList[index].c
enhzflep 31-Oct-12 7:17am
Of course mate. Have a look at the last line in the loop of method1 you just index the element you want, then use the . operator to access the field. Or if you preffered to use an iterator, rather than array notation to get to the element, you'd access the field using the -> operator. Did you try swapping the function definition for method1 for the commented-out one, like I suggested?
Tarun Batra 31-Oct-12 7:19am
no will try swapping
Tarun Batra 31-Oct-12 8:54am
can you plz reply on this sir
Tarun Batra 31-Oct-12 7:04am
Sir can you please tell if some time in future i want to update status field in the structure that has been added into the list how can i do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180621.3 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100