All Questions

Tagged with
Filter by
Sorted by
Tagged with
311 votes
17 answers
389k views

vector vs. list in STL

I noticed in Effective STL that vector is the type of sequence that should be used by default. What's does it mean? It seems that ignore the efficiency vector can do anything. Could anybody ...
skydoor's user avatar
  • 25.5k
287 votes
15 answers
302k views

Can you remove elements from a std::list while iterating through it?

I've got code that looks like this: for (std::list<item*>::iterator i = items.begin(); i != items.end(); i++) { bool isActive = (*i)->update(); //if (!isActive) // items.remove(*...
AShelly's user avatar
  • 35k
139 votes
9 answers
104k views

What's the difference between deque and list STL containers?

What is the difference between the two? I mean the methods are all the same. So, for a user, they work identically. Is that correct??
Lazer's user avatar
  • 92.4k
111 votes
2 answers
91k views

how to append a list<T> object to another

in C++, I have two list<T> objects A and B and I want to add all the members of B to the end of A. I've searched a few different sources and haven't found a simple solution (e.i. A.append(B);) ...
BCS's user avatar
  • 76.9k
105 votes
5 answers
326k views

How to get a certain element in a list, given the position?

So I've got a list: list<Object> myList; myList.push_back(Object myObject); I'm not sure but I'm confident that this would be the "0th" element in the array. Is there any function I can use ...
Test's user avatar
  • 1,051
86 votes
5 answers
65k views

QVector vs QList

I have a list of integers that I need to iterate over but an array is inadequate. What are the differences between vectors and lists and is there anything I need to know before I pick a type? Just to ...
jcuenod's user avatar
  • 57k
82 votes
6 answers
298k views

Pointer to incomplete class type is not allowed

For some reason I cannot use functions attached to the object I want to use. I added a comment to the line that is not working. As an error I get "Error; pointer to incomplete class type is not ...
Sharpless512's user avatar
  • 3,142
68 votes
8 answers
28k views

Is list::size() really O(n)?

Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to some sources, this is in fact implementation dependent as the standard doesn't say what the ...
foraidt's user avatar
  • 5,619
60 votes
3 answers
54k views

How get next (previous) element in std::list without incrementing (decrementing) iterator?

Say I have an std::list<int> lst and some std::list<int>::iterator it for iterating through the list. And depended to value of the it I want to use it + 1 or it - 1 in my code. Is there ...
Mihran Hovsepyan's user avatar
55 votes
8 answers
94k views

array vs vector vs list

I am maintaining a fixed-length table of 10 entries. Each item is a structure of like 4 fields. There will be insert, update and delete operations, specified by numeric position. I am wondering which ...
jasonline's user avatar
  • 8,876
55 votes
2 answers
110k views

How to initialize member-struct in initializer list of C++ class?

I have the following class definitions in c++: struct Foo { int x; char array[24]; short* y; }; class Bar { Bar(); int x; Foo foo; }; and would like to initialize the "foo" struct (...
Jan Rüegg's user avatar
  • 9,787
54 votes
5 answers
96k views

How to make the for each loop function in C++ work with a custom class

I'm new to C/C++ programming, but I've been programming in C# for 1.5 years now. I like C# and I like the List class, so I thought about making a List class in C++ as an exercise. List<int> ls; ...
Ricardo Pieper's user avatar
51 votes
15 answers
100k views

Cleaning up an STL list/vector of pointers

What is the shortest chunk of C++ you can come up with to safely clean up a std::vector or std::list of pointers? (assuming you have to call delete on the pointers?) list<Foo*> foo_list; I'd ...
twk's user avatar
  • 17.1k
42 votes
3 answers
30k views

Move list element to the end in STL

I have already the list pointer of CDrawObject* std::list<CDrawObject*> elements; How I can move some element to the end of list. I see STL Algorithms Reference but i don't find this ...
G-71's user avatar
  • 3,666
42 votes
4 answers
209k views

How to get an element at specified index from c++ List

I have a list: list<Student>* l; and I would like to get an element at a specified index. Example: l->get(4)//getting 4th element Is there a function or method in list which enables it ...
user2398815's user avatar
41 votes
5 answers
61k views

How do you iterate backwards through an STL list?

I'm writing some cross-platform code between Windows and Mac. If list::end() "returns an iterator that addresses the location succeeding the last element in a list" and can be checked when ...
AlanKley's user avatar
  • 4,682
40 votes
4 answers
34k views

Why isn't there an operator[] for a std::list?

Can anyone explain why isn't the operator[] implemented for a std::list? I've searched around a bit but haven't found an answer. It wouldn't be too hard to implement or am I missing something?
Gab Royer's user avatar
  • 9,667
39 votes
8 answers
55k views

is there in C# a method for List<T> like resize in c++ for vector<T>

When I use resize(int newsize) in C++ for vector<T>, it means that the size of this vector are set to newsize and the indexes run in range [0..newsize). How to do the same in C# for List<T>...
nhtrnm's user avatar
  • 1,549
35 votes
2 answers
36k views

Using hierarchy in findContours () in OpenCV?

When finding contours, I used the CV_RETR_CCOMP argument. This is supposed to create a two level hierarchy - the the first level are for outer contours, the second level are for boundaries of the ...
fdh's user avatar
  • 5,316
32 votes
1 answer
6k views

Complexity of std::list::splice and other list containers

I have some code which deals with various std::list objects, and I am currently using a very inefficient method of transferring content between them (I'm iterating through arbitrary sections of one ...
JBentley's user avatar
  • 6,159
29 votes
8 answers
190k views

How could I create a list in c++?

How can I create a list in C++? I need it to create a linked list. How would I go about doing that? Are there good tutorials or examples I could follow?
jessemiel's user avatar
  • 373
29 votes
6 answers
26k views

Does std::list::remove method call destructor of each removed element?

I have the code: std::list<Node *> lst; //.... Node * node = /* get from somewhere pointer on my node */; lst.remove(node); Does the std::list::remove method call the destructor (and free ...
Siarhei Fedartsou's user avatar
29 votes
3 answers
19k views

Sort list using STL sort function

I'm trying to sort a list (part of a class) in descending order containing items of a struct, but it doesn't compile: error: no match for 'operator-' in '__last - __first' sort(Result.poly.begin(),...
Vlad's user avatar
  • 618
27 votes
4 answers
18k views

Pass, return and convert to vectors list of lists over JNI

I need to pass from Java List< List<MyPoint> > points; over jni to C++ and convert to std::vector< std::vector<MyPoint> > Process this vectors and return List< List&...
George's user avatar
  • 949
25 votes
6 answers
40k views

C++ STL list vs set

Which of those two is faster for random insertions and deletions? My guess is list. Though having the values as the keys as in the case of sets is attractive too. Is performance similar for iterating ...
user240137's user avatar
25 votes
6 answers
35k views

How to access the 'previous' element in a C++ list iterator loop?

I'm trying to access the previously iterated element in a loop going through all the elements of a list. To be more specific, my loop looks like this: for (iter=list_object.begin(); iter!= ...
R.J.'s user avatar
  • 1,443
24 votes
3 answers
37k views

Erasing while iterating an std::list

If I'm using an iterator in a for loop and I use erase on a current iteration of iterator, the for loop should continue fine and access the rest of the list elements? From what I have read, this ...
johnbakers's user avatar
  • 24.4k
23 votes
2 answers
3k views

Sorting a std::list using iterators

Is it possible to sort portion of a list (subset of a list) defined by iterators like std::sort does? i.e with a std::list the only sort available is via a method (http://en.cppreference.com/w/cpp/...
lfgtm's user avatar
  • 1,047
22 votes
6 answers
45k views

Python equivalent for C++ STL vector/list containers

Is there something similar in Python that I would use for a container that's like a vector and a list? Any links would be helpful too.
pandoragami's user avatar
  • 5,495
22 votes
7 answers
5k views

How to initialise a STL vector/list with a class without invoking the copy constructor

I have a C++ program that uses a std::list containing instances of a class. If I call e.g. myList.push_back(MyClass(variable)); it goes through the process of creating a temporary variable, and then ...
Warpspace's user avatar
  • 3,345
21 votes
5 answers
15k views

How do I parallelize a for loop through a C++ std::list using OpenMP?

I would like to iterate through all elements in an std::list in parallel fashion using OpenMP. The loop should be able to alter the elements of the list. Is there a simple solution for this? It seems ...
mshang's user avatar
  • 965
20 votes
1 answer
14k views

invalid operator < while sorting std::list

I have a std::list graph edges and i want to sort the edges based on their destination outdegree and then their indegree. But i am getting getting exception of invalid operator < during my ...
Abdul Samad's user avatar
  • 5,838
20 votes
5 answers
17k views

Is there an equivalent of vector::reserve() for an std::list?

I have a class that looks like this: typedef std::list<char*> PtrList; class Foo { public: void DoStuff(); private: PtrList m_list; PtrList::iterator m_it; }; The function DoStuff() ...
Eitan T's user avatar
  • 32.8k
19 votes
2 answers
28k views

sorting std::lists using std::sort [duplicate]

Possible Duplicate: Sort list using stl sort function My question is can we sort two std::lists using std::sort function? I have 2 string lists std::list<std::string>list1, list2; .....//...
Aneesh Narayanan's user avatar
19 votes
5 answers
24k views

Memory leak with std::string when using std::list<std::string>

I'm working with std::list<std::string> in my current project. But there is a memory leak somewhere connected with this. So I've tested the problematic code separately: #include <iostream>...
Rock's user avatar
  • 1,216
19 votes
6 answers
15k views

Is returning a std::list costly?

I was wondering if returning a list, instead of returning a pointer to one, was costly in term of performance because if I recall, a list doesn't have a lot of attributes (isn't it something like 3 ...
Gab Royer's user avatar
  • 9,667
19 votes
5 answers
28k views

Python list equivalent in C++?

Could you please tell me what is the closest data type in C++ to python list? If there is nothing similar, how would you build it in C++?
gen's user avatar
  • 9,740
19 votes
4 answers
67k views

C++ pass list as a parameter to a function

I'm trying to build a very simple address book. I created a Contact class and the address book is a simple list. I'm trying to build a function to allow the user to add contacts to the address book. ...
Adrian's user avatar
  • 609
18 votes
7 answers
43k views

C++ STL: list with Pointers - Iterator cannot access?

I am struggeling with an STL list that holds Pointers of my "Object" object. I declared: list<Object*> objectlist; and inserted via: this->objectlist.push_back(new Object(address,value,...
Stefan's user avatar
  • 2,593
17 votes
2 answers
2k views

Why isn't std::list.size() constant-time? [duplicate]

This code ran for 0.012 seconds: std::list<int> list; list.resize(100); int size; for(int i = 0 ; i < 10000; i++) size = list.size(); This one for 9.378 seconds: std::list<int&...
Pavel Madaj's user avatar
16 votes
4 answers
45k views

List Iterator Remove()

I have a list iterator that goes through a list and removes all the even numbers. I can use the list iterator to print out the numbers fine but I cannot use the list's remove() and pass in the ...
Steve's user avatar
  • 12.1k
16 votes
2 answers
1k views

Use std::experimental::optional to implement a list

I was wondering if it is possible to implement a single (and possible double) linked list using std::experimental::optional. template <typename T> struct node { std::experimental::optional&...
fuji's user avatar
  • 1,183
16 votes
6 answers
38k views

Getting a list of values from a map

Is there an stl way to get a list of values from a map? i.e, I have: std::map<A,B> myMap; and I would like a function that will return just the list of values, i.e, std::list<B> (or set ...
Amir Rachum's user avatar
16 votes
2 answers
35k views

How to access the first element of std::list?

I have a list std::list<T *> *l;. this list is not null and has some values. My problem is how to access items properly? i do not need to iterate over the list. i just want the first item only. ...
shan's user avatar
  • 1,164
15 votes
2 answers
26k views

c++11 sorting list using lambda

While practicing the use of lambdas, I wrote this program which is supposed to sort a list of pairs by their second element (an int). #include <iostream> #include <algorithm> #include <...
punksta's user avatar
  • 2,788
15 votes
1 answer
18k views

Use nlohmann json to unpack list of integers to a std::vector<int>

I'm using nlohman::json. It's awesome, but is there any way to unpack: { "my_list" : [1,2,3] } into a std:vector<int> ? I can't find any mention in the docs, and std::vector<...
P i's user avatar
  • 29.8k
15 votes
1 answer
1k views

Using MMU to implement resizable arrays

Usually, lists are implemented either as linked lists, which are slow to traverse, or as array lists, which are slow when inserting elements. I was wondering if it would be possible to use the ...
d9584's user avatar
  • 153
15 votes
1 answer
11k views

Iterator equivalent to null pointer?

In an algorithm I'm currently implementing, I need to manipulate a std::list of struct T. T holds a reference to another instance of T, but this reference can also be "unassigned". At first, I wanted ...
Mikarnage's user avatar
  • 893
15 votes
6 answers
4k views

Cache-friendliness std::list vs std::vector

With CPU caches becoming better and better std::vector usually outperforms std::list even when it comes to testing the strengths of a std::list. For this reason, even for situations where I need to ...
Hatted Rooster's user avatar
14 votes
1 answer
11k views

How to handle list in R to Rcpp

I have a list in R that x<-list(c(1,2,3), c(4,5), c(5,5), c(6)). I want to input the list to Rcpp and return them as an average vector, c(2, 4.5, 5, 6). I am not sure how to handle the list in ...
user1690124's user avatar

1
2 3 4 5
58