Remove last 5 elements of a map - c++

I want to remove last 5 elements from a std::map.
One way is:
for(int i=0; i < 5; i++)
{
map<string, LocationStruct>::iterator it = myLocations.end();
it--;
myLocations.erase(it);
}
Is there a good way to do that without looping?
Thanks,

a compilable demo of one way to do it. Note that because map iterators are not random access iterators, there's likely to be a loop involved under the covers during the call to std::prev() anyway.
#include <iostream>
#include <map>
#include <string>
using namespace std::string_literals;
std::map<int, std::string> m = {
{ 0, "zero"s },
{ 1, "one"s },
{ 2, "two"s },
{ 3, "three"s },
{ 4, "four"s },
{ 5, "five"s }
};
auto main() -> int
{
for (const auto& entry : m) {
std::cout << entry.first << ", " << entry.second << std::endl;
}
// erase operation here
m.erase(std::prev(m.end(), 5), m.end());
std::cout << "\nafter erase \n\n";
for (const auto& entry : m) {
std::cout << entry.first << ", " << entry.second << std::endl;
}
return 0;
}
expected output:
0, zero
1, one
2, two
3, three
4, four
5, five
after erase
0, zero

You can use the std::prev function to do navigation for you:
m.erase(prev(m.end(), 5), m.end());

Use range erase
auto i = m.begin();
std::advance(i, (m.size() - 5) );
m.erase( i, m.end() );

As far as I know, std::map has normal access iterator, so you have to traverse to last element every time to erase it from memory.

Related

Removing a comma from a c++ output

I wrote this program that sorts numbers in vectors from greatest to least, it works great but the only thing that is bugging me is trying to remove the comma from the last number. Heres my code
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main() {
vector<int> vi1, vi2, vi3;
srand(987);
for (int i = 0; i < 10; ++i) vi1.push_back(rand() % 10);
sort(vi1.begin(), vi1.end());
for (int i = 0; i < 10; ++i) vi2.push_back(rand() % 10);
sort(vi2.begin(), vi2.end())
while(!vi1.empty() && !vi2.empty()) {
if(vi1.back()>=vi2.back()) {
vi3.push_back(vi1.back());
vi1.pop_back();
}
else {
vi3.push_back(vi2.back());
vi2.pop_back();
}
}
while(!vi1.empty()) {
vi3.push_back(vi1.back());
vi1.pop_back();
}
while(!vi2.empty()) {
vi3.push_back(vi2.back());
vi2.pop_back();
}
for (auto i = vi3.begin(); i != vi3.end(); ++i)
cout << *i << ", ";
cout << "\nBye..." << endl;
return 0;
}
and here is the output
9, 9, 9, 8, 8, 8, 6, 6, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0,
Bye...
as you can see after the last 0 there is a comma which doesn't make sense grammatically speaking. How can I remove that comma?
Rather than removing the simplest way is just to print commas only for other values than the first:
for (auto i = vi3.begin(); i != vi3.end(); ++i) {
if(i != vi3.begin()) {
cout << ", ";
}
cout << *i;
}
I am using this idiom regularly to format e.g. SQL parameter lists, or similar where a delimiter isn't wanted after the last element but for any others.
There are other ways to detect the first element (e.g. using a bool variable initialized to true before the loop starts, and set to false upon the first iteration).
For your example it seems just checking for vi3.begin() is the easiest and most natural way.
Here's the generic variant in pseudo code:
bool isFirstOutput = true;
for_each(element in list) {
if(not isFirstOutput) {
print delimiter;
}
print element;
isFirstOutput = false;
}
If begin, end are at least bidirectional iterators (as yours are),
you can just check initially that begin != end; if not print nothing; if so print
", "-puncuated elements incrementing begin != std::prev(end), and
finally print *begin(). like so:
main.cpp
#include <utility>
template<typename OStream, typename BiIter, typename Punct>
void punctuated(OStream && out, Punct && punct, BiIter begin, BiIter end)
{
if (begin != end) {
for (--end ;begin != end; ++begin) {
std::forward<OStream>(out) << *begin << std::forward<Punct>(punct);
}
std::forward<OStream>(out) << *begin;
}
}
#include <iostream>
#include <vector>
#include <list>
#include <set>
int main()
{
std::vector<int> v0;
std::vector<int> v1{{1}};
std::list<int> l{{1,2}};
std::set<int>s{{3,4,5,6,7}};
std::cout << "v0 [";
punctuated(std::cout,",",v0.begin(),v0.end());
std::cout << "]\n";
std::cout << "v1 [";
punctuated(std::cout,",",v1.begin(),v1.end());
std::cout << "]\n";
std::cout << "l [";
punctuated(std::cout,",",l.begin(),l.end());
std::cout << "]\n";
std::cout << "s [";
punctuated(std::cout,"|",s.begin(),s.end());
std::cout << "]\n";
return 0;
}
Output:
g++ -Wall -Wextra main.cpp && ./a.out
v0 []
v1 [1]
l [1,2]
s [3|4|5|6|7]
Live

C++ Vector Sorts

I am working with a vector in Codefights for the "almostIncreasingSequence" challenge. Is there a way to track how many "steps" a sort method takes when so I can set a simple counter/flag to check against if the sort passes a predefined threshold?
Yes there is. You can use a custom comparison function with std::sort as follows:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> myVector { 2, 8, 5, 9, 3, 7, 1, 4, 6, 0 };
int counter = 0;
std::sort(myVector.begin(), myVector.end(), [&counter](int lhs, int rhs) {
counter++;
return lhs < rhs;
});
std::cout << "Steps: " << counter << std::endl;
for(auto e : myVector)
std::cout << e << ' ';
std::cout << std::endl;
return 0;
}

STL correct use of find_if() to print out odd numbers

How can I possibly make use of the find_if algorithm from STL to find and print out odd numbers from a vector?
Let me just give you an example of what I'm on about:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool isOdd(int x)
{
return x%2 == 0;
}
int main(void)
{
int tab[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> myVec(tab, tab + sizeof(tab)/sizeof(tab[0]));
vector<int>::iterator it;
// Printing out all numbers
cout << "Vector contains the following numbers: " << endl;
for(it = myVec.begin(), it != myVec.end(), ++it)
{
cout << *it << ' ';
}
// An unsuccessful attempt to print out odd numbers while using find_if and while loop
vector<int>::iterator bound = find_if(myVec.begin(), myVec.end(), isOdd);
while(bound != myVec.end())
{
cout << *bound << ' ';
}
}
What is wrong with while loop? I guess it's the core problem of my code.
I'm assigning whatever the find_if function will return to the iterator, and then I simply can't figure out how to cherry-pick odd values from the vector ;(
The problem is that you are not advancing the iterator in your loop:
while(bound != myVec.end())
{
cout << *bound << ' ';
bound = find_if(bound+1, myVec.end(), isOdd);
}
In C++11 you can use std::next(bound) instead of bound+1.
Also, your isOdd returns true when the number is even. It should be
bool isOdd(int x)
{
return x%2 != 0;
}
Demo.
Just adding that for this use I'd just use std::copy_if:
std::copy_if(myVec.begin(), myVec.end(),
std::ostream_iterator<int>(std::cout, " "), isOdd);
Similarly, the first for loop in your code (and those should be semicolons, not commas) can be replaced with std::copy:
std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<int>(std::cout, " "));
Demo.

Using erase-remove idiom on non-unique collections

#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
enum class En{A, B};
std::vector<En> vec{En::A, En::B, En::A, En::B, En::A, En::B, En::A};
for(const auto& i : vec) std::cout << int(i) << ", ";
std::cout << std::endl;
vec.erase(std::remove(std::begin(vec), std::end(vec), vec.front()),
std::end(vec));
for(const auto& i : vec) std::cout << int(i) << ", ";
std::cout << std::endl;
return 0;
}
Ideone: http://ideone.com/NTPVyE
Prints:
0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 0,
Why is this happening? Shouldn't only the first element of the vector be removed?
I suppose std::remove doesn't stop at the first element, but runs through the whole vector. Is there any way I can use the erase-remove idiom on collections with non-unique elements?
The third argument to std::remove is a const reference. You are changing the value of the element refered to as you move elements around, leading to undefined behaviour.
This would be well behaved:
auto elem = vec.front(); // copy front element
vec.erase(std::remove(std::begin(vec), std::end(vec), elem),
std::end(vec));
Output:
1, 1, 1,

Is there any way to output the actual array in c++

So, I'm beginning C++, with a semi-adequate background of python. In python, you make a list/array like this:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Then, to print the list, with the square brackets included, all you do is:
print x
That would display this:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
How would I do the exact same thing in c++, print the brackets and the elements, in an elegant/clean fashion? NOTE I don't want just the elements of the array, I want the whole array, like this:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
When I use this code to try to print the array, this happens:
input:
#include <iostream>
using namespace std;
int main()
{
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << anArray << endl;
}
The output is where in memory the array is stored in (I think this is so, correct me if I'm wrong):
0x28fedc
As a sidenote, I don't know how to create an array with many different data types, such as integers, strings, and so on, so if someone can enlighten me, that'd be great!
Thanks for answering my painstakingly obvious/noobish questions!
You can write a simple helper function to allow you to stream the array to an output stream (including but not limited to std::cout):
#include <iostream>
// print an array to an output stream
// prints to std::cout by default
template <typename T, std::size_t N>
void print_array(const T(&a)[N], std::ostream& o = std::cout)
{
o << "{";
for (std::size_t i = 0; i < N-1; ++i)
{
o << a[i] << ", ";
}
o << a[N-1] << "}\n";
}
where a function template is used in order to deduce both the type and size of the array at compile time. You can use it like this:
#include <fstream>
int main()
{
int a[] = {1,2,3,4,5};
print_array(a); // prints {1, 2, 3, 4, 5} to stdout
std::string sa[] = {"hello", "world"};
print_array(sa, std::cerr); // prints {hello, world} to stderr
std::ofstream output("array.txt");
print_array(a, output); // prints {1, 2, 3, 4, 5} to file array.txt
}
This solution can be trivially generalized to deal with ranges and standard library containers. For even more general approaches, see here.
As for the side note, you cannot do that in C++. An array can only hold objects of one type.
Inspired by the answers of juanchopanza and Raxman I decided to do a real IO manipulator, which leads to a syntax like:
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even " << print(std::vector<int> { 1,2,3,42 }, ":") << "!\n";
printing
Woot, I can has { hello, bye } and even { 1:2:3:42 }!
Note
it works seamlessly with chained output streaming using operator<< as usual
it is fully generic (supporting any container of streamable types)
it even allows to pass a delimiter (as an example)
with a little more template arguments it could be made so generic as to work with ostream, wostream etc.
fun: Since the delimiter can be any streamable 'thing' as well, you could even... use an array as the delimiter:
std::cout << "or bizarrely: " << print(arr, print(arr)) << "\n";
resulting in the rather weird sample output:
or bizarrely: { hello{ hello, bye }bye }
Still demonstrates the power of hooking seamlessly into IO streams, if you ask me.
I believe it will not get much more seamless than this, in C++. Of course there is some implementing to do, but as you can see you can leverage full genericity, so you're at once done for any container of streamable types:
#include <iostream>
#include <vector>
namespace manips
{
template <typename Cont, typename Delim=const char*>
struct PrintManip {
PrintManip(Cont const& v, Delim d = ", ") : _v(v), _d(std::move(d)) { }
Cont const& _v;
Delim _d;
friend std::ostream& operator<<(std::ostream& os, PrintManip const& manip) {
using namespace std;
auto f = begin(manip._v), l(end(manip._v));
os << "{ ";
while (f != l)
if ((os << *f) && (++f != l))
os << manip._d;
return os << " }";
}
};
template <typename T, typename Delim=const char*>
manips::PrintManip<T, Delim> print(T const& deduce, Delim delim = ", ") {
return { deduce, std::move(delim) };
}
}
using manips::print;
int main()
{
const char* arr[] = { "hello", "bye" };
std::cout
<< "Woot, I can has " << print(arr)
<< " and even: " << print(std::vector<int> { 1,2,3,42 }, ':') << "!\n"
<< "or bizarrely: " << print(arr, print(arr)) << "\n";
}
See it live at http://ideone.com/E4G9Fp
for(int i=0;i<9;i++)
cout << anArray[i] << endl;
ahh ok with brackets it be such (simply array print logic for your arrays , u can make it more general in future)
cout<<'{';
for(int i=0;i<8;i++)
cout << anArray[i] <<',';
cout<<anArray[8]<<'}';
For python users and c++ lovers there is std::vector .
here how it be print logic for vector
//solution with [] operator
if(anVector.size()>=1){
std::cout<<"{";
for(int i=0;i<anVector.size()-1;i++){
std::cout<<anVector[i]<<',' ;
}
std::cout<<anVector[anVector.size()-1]<<'}' ;
}
//solution with iterator
std::vector<int>::iterator it =anVector.begin();
if(it!=anVector.end()){
std::cout << '{'<<*it;
++it;
for (; it != anVector.end(); ++it){
std::cout<<','<< *it ;
}
std::cout << '}';
}
Also check C++ 11 std::vector . In new standart initializing and other things more elegant
Probably the easiest way to get an array printed nicely (assuming it has a length greater than zero) is with something like:
std::cout << "{" << anArray[0];
for (int i = 1; i < sizeof (anArray) / sizeof (*anArray); i++)
std::cout << ", " << array[i];
std::cout << "}";
If you wish to be able to print less than the full array, you'll need a way to specify the length (which may be zero so you should handle that case:
if (length == 0)
std::cout << "{}";
else {
std::cout << "{" << anArray[0];
for (int i = 1; i < length; i++)
std::cout << ", " << array[i];
std::cout << "}";
}
There may be other variations of that such as printing at a given starting point rather than element zero but I won't go into that here. Suffice to say, it's just a small modification to the loop and if condition.
Of course, if you want to do it the simple way, with std::cout << myvariable;, you can consider wrapping the whole thing in a class and providing your own operator<< for it - that would be a more object-oriented way of doing things.
If you don't care too much about having the comma as a separator, you could also use output iterators.
#include <iostream>
#include <iterator>
#include <algorithm>
...
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "{ ";
std::copy(anArray, anArray + 9, std::ostream_iterator<int>(std::cout, " "));
std::cout << "}" << std::endl;
Another idea to achieve this, is to define a macro to convert c-arrays to std::vector and a template function to output a std::vector, like this:
#include <vector>
#include <iostream>
// convert an array (e.g. int32_t x[20]) into an std::vector
#define ARRAY_TO_STD_VECTOR(VAR, TYPE) std::vector<TYPE>(VAR, VAR + sizeof(VAR)/sizeof(TYPE))
// output of std::vector<T>
namespace std {
template<typename T>
std::ostream& operator<<(std::ostream& p, const std::vector<T>& v)
{
p << "{";
for (size_t i = 0; i < v.size(); ++i)
{
p << v[i];
if (i < (v.size() - 1)) p << ", ";
}
p << "}";
return p;
}
}
Having this, you can output your array like this:
int main()
{
int anArray[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << ARRAY_TO_STD_VECTOR(anArray, int) << endl;
}