Questions tagged [std]

The C++ Standard Library, and its namespace. Use in conjunction with [c++].

From Wikipedia

The C++ Standard Library provides several generic containers, functions to utilise and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O).

http://en.wikipedia.org/wiki/C++_Standard_Library

The concept of using namespaces

http://en.wikipedia.org/wiki/Namespace_(computer_science)

Also see

4970 questions
3
votes
1 answer

Why didn't std::ofstream in recursion work as expected?

Given codes as below: int recur(int num); int main() { recur(5); return 0; } int recur(int num) { static unsigned count = 0; //static std::ofstream log("log.txt",std::ios_base::app|std::ios_base::out); …
Yue Wang
  • 1,710
  • 3
  • 18
  • 43
3
votes
2 answers

How do you get the number of elements in a std::map as an unsigned long?

How do you get the number of elements in a std::map as an unsigned long? Assuming you have an object like this: std::map myNightmare; I have been trying to figure out how to get its number of elements. You see, I need…
Alexandru
  • 12,264
  • 17
  • 113
  • 208
3
votes
2 answers

is there std::mismatch for two unequal sized vectors?

I d like to compare two vectors where the second one might have more/less items than the first one. v1 = 1,2,3,4,5 v2 = 1,0,3,4,5,6 As far as I understood, std::mismatch woNT do the trick. How could I detect the missing element in the v1? Thanks…
Orkun
  • 6,998
  • 8
  • 56
  • 103
3
votes
4 answers

namespace usage

I'm trying to start using namespaces the correct (or at least best) way. The first thing I tried to do was to avoid putting using namespace xxx; at the beginning of my files. Instead, I want to using xxx::yyy as locally as possible. Here is a small…
Jérôme
  • 26,567
  • 29
  • 98
  • 120
3
votes
2 answers

How to insert after the last element of a resized std::vector?

I know how much space I initially need in a std::vector. So I use .resize() to set the new vector to that size. But when using .push_back() afterwards, it adds the elements at the end of the allocated size increasing it by one. How can I add new…
danijar
  • 32,406
  • 45
  • 166
  • 297
3
votes
2 answers

How to correctly fill a list of list in c++

Consider the following code: #include #include using namespace std; int main(int argc, const char * argv[]) { list l{1,2,3,4}; list> ll; ll.push_back(l); return 0; } After the push_back, the ll list…
Kevin MOLCARD
  • 2,168
  • 3
  • 22
  • 35
3
votes
3 answers

How to find the positions of an item in a std::vector

My question is very similar to How to find an item in a std::vector? However, I want to go further, suppose the item I am searching for appears several times in the vector, and I want to obtain its positions in the vector as well. For example, the…
feelfree
  • 11,175
  • 20
  • 96
  • 167
3
votes
2 answers

overload operator< in using std::set

It's the first time I use the std::set container and I have a problem with the operator std::less. I declare the set : std::set > _set; Then, I overload the operator< for MyClass ; the problem seems to be linked the mix…
federem
  • 299
  • 1
  • 6
  • 17
3
votes
1 answer

Why is map::operator[] slow by design?

The problem: Peope complain about this: In STL maps, is it better to use map::insert than []? When accessing std::map data; data[key1] // <-- Calls default constructor each time it is called, //…
Sam
  • 19,708
  • 4
  • 59
  • 82
3
votes
4 answers

Copy members to std::vector

struct Node { std::string name; ... }; typedef std::vector Nodes; Nodes nodes; std::vector names; Is there a nice one-liner way of populating the vector names with Node::name for each item in nodes? This is what I…
Baz
  • 12,713
  • 38
  • 145
  • 268
3
votes
2 answers

How is std::cin >> std::string implemented?

In particular, how do the code check if memory for chars should be reallocated? Or how many chars the user entered? If I wanted to assign a C-string's value to my implementation of a string class I would probably do something like this String&…
Anhil
  • 65
  • 1
  • 6
3
votes
1 answer

How to instantiate an comparison function (functor) when defining a map/set?

I'm using a funciton object to specify a comparison function for map/set: struct CompareObject { bool operator()(Node* const n1, Node* const n2) const; }; As far as I understand defining a set like this will not create any instance of…
msgmaxim
  • 788
  • 2
  • 8
  • 15
3
votes
4 answers

Partially match long key in std::map

I'm using a std::map in my project, because I want to map several different strings to each other. For example I might create a map that looks similar to this: std::map map; map["test"] = "Foo"; map["blah"] =…
Elliott Darfink
  • 1,153
  • 14
  • 34
3
votes
4 answers

Pointer list c++

The code below: #include #include class A { public: void printHello(){std::cout << "hello";} }; int main(int argc, char *argv) { std::list lista; lista.push_back(new A()); for(std::list::iterator…
user1519221
  • 631
  • 5
  • 13
  • 24
3
votes
2 answers

Specializing templates in namespaces - is namespace alias really an alias?

I'm trying to provide a hash<> specialization for a family of types I've been working on. So far so good, the specialization itself is easy to provide, I've already done similar things for numeric_limits<>. But I'm facing the problem of how to…
Luis Machuca
  • 1,047
  • 9
  • 16