Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Visual-Studio STL
Mine this structure is global an di added it into the stl
 
struct Fun
{
    int a;
    char* c;
}FUN;
 
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 10:06am
Edited 30-Oct-12 12:03pm
v2

1 solution

Rate this: bad
good
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. Smile | :)
 
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;
}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);
}
  Permalink  
v2
Comments
Eugen Podsypalnikov at 30-Oct-12 15:52pm
   
3 stars - for three memory leaks :)
enhzflep at 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 at 30-Oct-12 17:26pm
   
Yes, I have got inclination to write the structure destructor properly :)
http://www.codeproject.com/Questions/486436/Howpluscanplusipluspassplustheplusthisplusstlplusl
enhzflep at 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 at 30-Oct-12 18:15pm
   
Very understandable solution :)
Tarun Batra at 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 at 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;
}
}
Tarun Batra at 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?
enhzflep at 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 at 31-Oct-12 7:19am
   
no will try swapping
enhzflep at 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
Tarun Batra at 31-Oct-12 8:54am
   
can you plz reply on this sir http://www.codeproject.com/Questions/486905/Howplustoplustoplusthispluspassingpluspointers-3f

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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