How do I reverse a C++ vector?
Is there a built-in vector function in C++ to reverse a vector in place? Or do you just have to do it manually? 7 Answers 7
Is there a built-in vector function in C++ to reverse a vector in place? Or do you just have to do it manually? 7 Answers 7
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 to quit program!”; cout << “Come up and C++ me some time.” << endl; printf(“Follow this command: %s”, myString); cin.get(); return 0; } Each … Read more
What are some really good reasons to ditch std::allocator in favor of a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom allocators have always been a feature of the Standard Library that I haven’t had much need for. I was … Read more
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 use the Standard C++ libraries. 16 Answers 16
I’m practicing using mulitple files and header files etc. So I have this project which takes two numbers and then adds them. Pretty simple. Here are my files: main.cpp #include <iostream> #include “add.h” int main() { int x = readNumber(); int y = readNumber(); writeAnswer(x + y); return(0); } io.cpp int readNumber() { int x; … Read more
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); //else other_code_involving(*i); } items.remove_if(CheckItemNotActive); I’d like remove inactive items immediately after update them, inorder to avoid walking the list again. But if I add the commented-out lines, I get an error when I get to i++: … Read more
C++11 vectors have the new function emplace_back. Unlike push_back, which relies on compiler optimizations to avoid copies, emplace_back uses perfect forwarding to send the arguments directly to the constructor to create an object in-place. It seems to me that emplace_back does everything push_back can do, but some of the time it will do it better … Read more
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. (…) it refers to the “STL”, despite the fact that very few people still use the STL (which was designed at … Read more
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(); 17 s 17 You can use std::find from <algorithm>: #include <algorithm> #include <vector> vector<int> vec; //can have other data types instead of int but … Read more
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 inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? … Read more