Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Mine this structure is global an di added it into the stl

C++
struct Fun
{
    int a;
    char* c;
}FUN;


i added it into the stl as follows
C++
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
Updated 30-Oct-12 11:03am
v2

1 solution

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:


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

struct Fun
{
    int a;
    char* c;
}FUN;

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");
    funList.push_back(myTemp);

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

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

    method1(funList);
    cout << endl;
    method2(funList);
    
    // EDIT:
    // free the memory we grabbed in the strdup calls.
    vecFunIter mIter;
    for (mIter=funList.begin(); mIter!=funList.end(); mIter++)
        free(mIter->c);
}
 
Share this answer
 
v2
Comments
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 :)
http://www.codeproject.com/Questions/486436/Howpluscanplusipluspassplustheplusthisplusstlplusl
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.
Eugen Podsypalnikov 30-Oct-12 18:15pm    
Very understandable solution :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900