有关C ++中STL的查询 [英] Queries regarding STL in C++

查看:77
本文介绍了有关C ++中STL的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该结构是全局结构,并将其添加到stl

Mine this structure is global an di added it into the stl

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



我将其添加到stl中,如下所示



i added it into the stl as follows

vector<FUN > SS;




现在我该怎么做以下
1)我通过列表指针访问结构的元素.
2)如果我想将列表作为函数的参数传递,那么结构的声明应该是什么.
3)如何从列表中一个接一个地获取节点.




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.

推荐答案

好吧,这是解决问题的一种方法.我已经通过引用传递了向量(将& char放在函数sig的var名称前面),因为我们可能在谈论兆字节的数据.无需复制所有数据并将其传递给函数-我们可以简单地告诉函数在哪里找到它.

我可以使用指针*或引用&
来完成此操作 在这种情况下非常重要.

尽管要知道-通过作为指针或引用传递,但是如果您在函数中修改输入参数,它将修改实际数据本身-而不仅仅是其副本.

您可以通过在打印方法后清除方法1中每个结构的值来进行尝试.现在注意方法2如何显示新值?如果您再删除&从功能签名重新尝试,是否没有效果?

另外,不应真正混用C/C ++代码-即FUN.c可能应该是字符串. 就目前而言,我不会取消分配通过strdup调用获得的内存-这会造成内存泄漏.但这是另一回事.我旨在使您的代码易于理解. :)

无论如何,事不宜迟.代码如下:


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;
}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);
}


这篇关于有关C ++中STL的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆