dwong
dwong

Reputation: 59

using structs in a STL list c++

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.

Upvotes: 1

Views: 33373

Answers (1)

Gille
Gille

Reputation: 5773

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

Upvotes: 4

Related Questions