【问题标题】:using structs in a STL list c++在 STL 列表 C++ 中使用结构
【发布时间】:2012-12-04 18:36:53
【问题描述】:

我是使用 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 个网站,只是为了看看这些列表是如何工作的。

【问题讨论】:

  • 这是我使用 c++ 的第二天,所以真的不知道那个网站在说什么,因为它没有明确的解释,并假设你完全了解 c++。你能解释一下使用 list a; 之间的区别吗?和 std::list?
  • @user1726053:如果这是你的第二天,你就有点过头了。这是上课的吗?
  • 什么意思? std::list&lt;int&gt; a;list&lt;int&gt; a;?前者更清楚您使用的是哪个list,后者需要某种形式的using 声明。
  • 哈哈哈上周的课程和教授让我们从 c 切换到 c++ 以完成最终项目。我知道我将如何在 C 中做到这一点,只是认为如果我使用 STL 列表会更容易。我的意思是我认真地认为只要学习如何使用 STL 实现列表并完成……语法简直要了我的命。

标签: c++ list stl


【解决方案1】:

你不需要初始化列表的大小,你可以使用std::list.push_back()std::list.push_front()向它添加新元素

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

这适用于所有事情: http://www.cplusplus.com/reference/

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

关于您在 cmets 中的问题, 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 test {
    string tmp;
};

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

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

【讨论】:

  • cplusplus.com 通常是不正确的并且有可怕的例子。请不要使用它。
  • @Tim jcatki.no-ip.org/fncpp/cplusplus.com 快速了解不正确之处。
  • @Gille:使用反引号来格式化代码,而不是 &lt;code&gt; 。此外,如果您可以输入示例代码来创建包含一些元素的列表然后打印它,这将对未来的读者有所帮助。
  • @Tim:他们修复了许多问题,但它仍然缺少 C++11 的大部分内容,而且它仍然被我们关闭且无法修复。此外,教程仍然教导不良做法。 Cppreference 具有 C++11 位,并且是一个 wiki,因此我们可以使其保持最新。但是你说得对,cplusplus 比以前好多了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 2014-04-05
  • 2010-11-08
  • 2018-11-13
相关资源
最近更新 更多