在STL列表中使用结构C ++ [英] using structs in a STL list c++

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

问题描述

我是使用c ++的新手,我真的不知道如何使用STL列表.我正在绘制城市街道的交叉点图.这是我的结构/头文件:

全局头文件

#ifndef GLOBAL_H
#define GLOBAL_H

typedef struct Vertex_ vertex;
typedef struct Edge_ ege;

#endif

顶点头

#ifndef VERTEX_H
#define VERTEX_H
 #include<list>
#include "global.h"
#include "edgelist.h"
struct Vertex_{
    int xsect;
    int danger;
    char xstreet[25];

    list<Edge_> EdgeList;
    struct Vertex_ *next;
    struct Vertex_ *prev;
};   

 #endif

边缘页眉

#ifndef EDGE_H
#define EDGE_H

#include "global.h"
#include "vertex.h"
struct Edge_{
    Vertex_ *adjvertex;
    int distance;

    struct Edge_ *next;
    struct Edge_ *prev;
};  

#endif

我的讲师没有给我们关于c ++的任何注释,所以真的不知道如何开始图表.这就是我开始主程序的想法:

#include<iostream>
#include<list>
#include "vertex.h"
#include "edge.h"
#include "global.h"
int main(){
   list<Vertex_> xsection;
   list<Edge_> EdgeList;
}

我必须扫描另一个文件中的数据,所以我真的不知道列表的大小.问题是我是否需要初始化列表的大小,或者是否可以仅使用迭代器添加内容.另一个问题是如何访问此列表元素中的数据.我是否只需使用迭代器并拥有:

*iter->EdgeList.begin(); 

如果有人拥有一个网站,该网站具有STL列表的所有命令和功能以及如何使用它,那真是太棒了,因为我目前有10个网站正在开放,目的只是为了查看这些列表的工作方式.

解决方案

您无需初始化列表的大小,只需使用std::list.push_back()std::list.push_front()

向列表中添加新元素即可

我喜欢此链接的列表: http://www.cplusplus.com/reference/list/list/

这适用于所有内容: http://www.cplusplus.com/reference/

关于cplusplus.com的一件好事是,如果标准定义了函数调用,它们会指定函数调用的复杂性.

关于您在评论中的问题, std是一个命名空间,您可以通过添加using namespace std;来导入它,因此您无需编写std::list.在C ++中,您可以有多个命名空间,每个命名空间都实现自己的list版本.

list表示包含类型为X的元素的模板列表. std::list是STL列表.

这是一个简单的例子:

int main(void) {
    std::list<std::string> l;
    l.push_back("overflow");
    l.push_back("test");

    /* Access it through iterators */
    /* iterators are kinda like pointers, but each ++ moves to the next item */
    std::list<std::string>::iterator it;
    for(it = l.begin(); it != l.end(); it++) {
        std::cout << "item: " << *it << std::endl;
    }

    l.push_front("stack");
    std::cout << *l.begin() << std::endl;

}

这是工作中的代码: http://ideone.com/R8sQhH

如果您使用的是struct:

struct test {
    string tmp;
};

void somefunction() {
    std::list<test> l;
    /* code */
    std::cout << (l.begin())->tmp << std::endl;
}

完整的结构示例: http://pastebin.com/YETUq1xT

I'm new to using c++ and I don't really know how to use the STL lists. I'm making a graph of intersections of city streets. Here's my structs/header files:

global header

#ifndef GLOBAL_H
#define GLOBAL_H

typedef struct Vertex_ vertex;
typedef struct Edge_ ege;

#endif

vertex header

#ifndef VERTEX_H
#define VERTEX_H
 #include<list>
#include "global.h"
#include "edgelist.h"
struct Vertex_{
    int xsect;
    int danger;
    char xstreet[25];

    list<Edge_> EdgeList;
    struct Vertex_ *next;
    struct Vertex_ *prev;
};   

 #endif

edge header

#ifndef EDGE_H
#define EDGE_H

#include "global.h"
#include "vertex.h"
struct Edge_{
    Vertex_ *adjvertex;
    int distance;

    struct Edge_ *next;
    struct Edge_ *prev;
};  

#endif

My instructor didn't give us any notes on c++ so don't really know how to start the graph. Here's how I was thinking of starting my main:

#include<iostream>
#include<list>
#include "vertex.h"
#include "edge.h"
#include "global.h"
int main(){
   list<Vertex_> xsection;
   list<Edge_> EdgeList;
}

I have to scan in the data from another file so I don't really know what the size of the list will be. The question is whether or not I need to initialize the size of the list or if I can just add stuff using an iterator. Another question is how do I access the data within the elements of this list. Would I just use an iterator and have:

*iter->EdgeList.begin(); 

If anyone has a website that has all the commands and functions for the STL list and how to use it that would be awesome because I currently have 10 websites open just to see how these lists work.

解决方案

You do not need to initialize the size of the list, you can just add new elements to it using std::list.push_back() or std::list.push_front()

I like this link for lists: http://www.cplusplus.com/reference/list/list/

And this for everything: http://www.cplusplus.com/reference/

A good thing about cplusplus.com is that they specify the complexity of function calls if this is defined by the standard.

About your question in the comments, std is a namespace and you can import it by adding using namespace std; so you don't need to write std::list. In C++ you can have multiple namespaces each implementing their own version of list.

list means a template list containing elements of type X. std::list is a STL list.

Here's a simple example:

int main(void) {
    std::list<std::string> l;
    l.push_back("overflow");
    l.push_back("test");

    /* Access it through iterators */
    /* iterators are kinda like pointers, but each ++ moves to the next item */
    std::list<std::string>::iterator it;
    for(it = l.begin(); it != l.end(); it++) {
        std::cout << "item: " << *it << std::endl;
    }

    l.push_front("stack");
    std::cout << *l.begin() << std::endl;

}

And here is the code at work: http://ideone.com/R8sQhH

And if you are using a struct :

struct test {
    string tmp;
};

void somefunction() {
    std::list<test> l;
    /* code */
    std::cout << (l.begin())->tmp << std::endl;
}

Complete struct example: http://pastebin.com/YETUq1xT

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

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