Advertisement

STL and use, anyone else help me?

Started by May 27, 2018 03:17 PM
7 comments, last by Gnollrunner 6 years, 3 months ago
Hi I read some tutorials on STL as many people on this forum say it's faster than the most selfwritten container classes and somehow i can't even declare a list in VC .net 2003...the compiler says: Compiling... VertexManager.cpp c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(33) : error C2143: syntax error : missing ';' before '<' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(33) : error C2501: 'CVertexManager::list' : missing storage-class or type specifiers c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(33) : error C2238: unexpected token(s) preceding ';' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(34) : error C2143: syntax error : missing ';' before '<' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(34) : error C2501: 'CVertexManager::list' : missing storage-class or type specifiers c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(34) : error C2238: unexpected token(s) preceding ';' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(35) : error C2143: syntax error : missing ';' before '<' c:\Dokumente und Einstellungen\gregor\Eigene Dateien\nGin\VertexManager.h(35) : error C2501: 'CVertexManager::list' : missing storage-class or type specifiers my code:

class CVertexManager {
private:
	list<VertListEntry> VertList;
	list<VertGroup>     VertGroup;
	list<int>           TextureChange;
   
	CVertexManager();
public:
	~CVertexManager();
	void addEntry(VertListEntry Entry);
	
	static CVertexManager& Instance() {
		static CVertexManager TheOneAndOnly;
		return CVertexManager;

	}

};

btw what does the list.insert function want as the first parameter? it says something with iterator...what is that? can i just pass an int as the number where i want to have the new entry? regards, m4gnus
 

How can it be faster ?, does it also use 2 dummys ?, guess not.

If you write your own list you can have dummys as first and last to make things faster.

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

Advertisement

1: why are you using VC .net 2003 ?

2: to use the standard library, you need the std:: prefix for each type or function you use from it. Try compiling this example and then using the same style in your code:


#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // Create a list containing integers
    std::list<int> l = { 7, 5, 16, 8 };
 
    // Add an integer to the front of the list
    l.push_front(25);
    // Add an integer to the back of the list
    l.push_back(13);
 
    // Insert an integer before 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // Iterate and print values of the list
    for (int n : l) {
        std::cout << n << '\n';
    }
}

3. Standard library containers require a decent understanding of the 'iterator' concept to use them effectively. The explanation here is pretty good: http://www.cplusplus.com/reference/iterator/

 

Not nice to downvote me while not saying why.

Does STD use 2 dummys then ?, no?, then its not faster.

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

2 hours ago, scullsold said:

as many people on this forum say [STL is] faster than the most selfwritten container classes

Yes, the this is generally true because the standard library is well featured and exhaustively tested.  Something you could write in less than an hour may be as fast or faster, but it may be error-prone, which may cost dozens of hours in debugging later down the line.

At the point a profiler were to determine that the container was slow, there are still many options to try before rewriting a container.

Stick with what works and move on.

 

Advertisement

I believe std::list must be a doubly linked list, with pointers to the head and tail, in order to meet the algorithmic complexity requirements in the language specification.

As for speed, linked lists involve extra allocations and are not cache friendly so whether using the standard one vs a hand rolled one, both would often be worse than using a dynamic array such as std::vector - though that really depends on your usage patterns.

This assumes that there are no higher level algorithmic options that would be better still.

On 5/27/2018 at 12:21 PM, Kylotan said:

3. Standard library containers require a decent understanding of the 'iterator' concept to use them effectively. The explanation here is pretty good: http://www.cplusplus.com/reference/iterator/

 

As an addendum to this, don't get the bright idea to write your own container that implements the std::iterator or something like that. The guts of the STL are arcane.

Also check ISO FAQ more on the STL. In fact reading that whole FAQ is a good exercise.

On 5/27/2018 at 1:58 PM, rip-off said:

I believe std::list must be a doubly linked list, with pointers to the head and tail, in order to meet the algorithmic complexity requirements in the language specification.

As for speed, linked lists involve extra allocations and are not cache friendly so whether using the standard one vs a hand rolled one, both would often be worse than using a dynamic array such as std::vector - though that really depends on your usage patterns.

This assumes that there are no higher level algorithmic options that would be better still.

Agreed; std::vector is just fine most of the time. For instance, Toady One has said most of the containers in Dwarf Fortress are just std::vector, and that game simulates the entire history of a fantasy world. You can always swap it out for something more appropriate (even arrays, but arrays are evil).

And yes, drop VC 2003. The latest VisualStudio Community is better, free, and more standards compliant.

I actually have my own library of containers that I've been using for years.  Whether the standard library is faster or not probably depends on the exact usage.  I like being able to customize things and I like to know exactly what's going on under the hood. That being said I would guess the standard library is pretty efficient for general usage.





 

This topic is closed to new replies.

Advertisement