How do you remove elements from a std::vector while iterating? [duplicate]

The erase() method returns a new (valid) iterator that points to the next element after the deleted one. You can use this iterator to continue with the loop: std::vector<std::string>::iterator iter; for (iter = m_vPaths.begin(); iter != m_vPaths.end(); ) { if (::DeleteFile(iter->c_str())) iter = m_vPaths.erase(iter); else ++iter; }

What is a C# analog of C++ std::pair?

Tuples are available since .NET4.0 and support generics: Tuple<string, int> t = new Tuple<string, int>(“Hello”, 4); In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following: public class Pair<T, U> { public Pair() { } public Pair(T first, U second) { this.First = first; this.Second = second; } public T First … Read more

Need memory efficient way to store tons of strings (was: HAT-Trie implementation in java)

The trie seems like a very good idea for your constraints. A “thinking outside the box” alternative: If you can afford some probability of answering “present” for a string that is absent EDIT: if you can afford false positives, use a Bloom filter as suggested by WizardOfOdds in the comments. For k=1, a Bloom filter … Read more

How do you know where to perform rotations in an AVL tree?

The pseudocode that you’ve posted will correctly balance a tree. That said, it is too inefficient to be practical – notice that you’re recursively exploring the entire tree trying to do rebalancing operations, which will make all insertions and deletions take O(n) time, eating away all the efficiency gains of having a balanced tree. The … Read more

Linked list vs. dynamic array for implementing a stack

There are many tradeoffs involved here and I don’t think that there’s a “correct” answer to this question. If you implement the stack using a linked list with a tail pointer, then the worst-case runtime to push, pop, or peek is O(1). However, each element will have some extra overhead associated with it (namely, the … Read more