All Questions

1797
votes
34answers
575779 views

Why is "using namespace std" considered bad practice?

I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std considered a bad practice? Is it ...
349
votes
4answers
63398 views

What's the difference between "STL" and "C++ Standard Library"?

Someone brought this article to my attention that claims (I'm paraphrasing) the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL. (...
139
votes
12answers
195434 views

Replace part of a string with another string

Is it possible in C++ to replace part of a string with another string? Basically, I would like to do this: QString string("hello $name"); string.replace("$name", "Somename"); But I would like to u...
447
votes
16answers
647709 views

How to find out if an item is present in a std::vector?

All I want to do is to check whether an element exists in the vector or not, so I can deal with each case. if ( item_present ) do_this(); else do_that();
120
votes
14answers
484920 views

C read file line by line

I wrote this function to read a line from a file: const char *readLine(FILE *file) { if (file == NULL) { printf("Error: file pointer is null."); exit(1); } int maximumLi...
203
votes
10answers
161933 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(*i); ...
40
votes
5answers
2684 views

Sorting zipped (locked) containers in C++ using boost or the STL

What I want to do: I want to sort 2, or 3, or N vectors, locked together, without copying them into a tuple. That is, leaving verbosity aside, something like: vector<int> v1 = { 1, 2, 3...
81
votes
14answers
54790 views

A std::map that keep track of the order of insertion?

I currently have a std::map<std::string,int> that stores an integer value to an unique string identifier, and I do look up with the string. It does mostly what I want, except for that it does no...
44
votes
5answers
38719 views

How is std::string implemented?

I am curious to know how std::string is implemented and how does it differ from c string?If the standard does not specify any implementation then any implementation with explanation would be great wit...
101
votes
7answers
203252 views

C++ printf with std::string?

My understanding is that string is a member of the std namespace, so why does the following occur? #include <iostream> int main() { using namespace std; string myString = "Press ENTER...

Previous Next