-2

I was translating a code from Python to C++ (of which i'm not a frequent user by far, understanding only a little more than just synthax) and got stuck at a point, where i need to format the b array and then remove the duplicates from already formatted array. At the very end, count the number of items in the array c. I have outline the problematic part (which i left written in Python). As you can see, i have a m_l array with 10 numbers, which will be used for 5 for loops. I have a preliminarily prepared b array where i will put all the possible sums comming out of the 5 for loop construction. Then, as it was mentioned earlier, i want to format items of b array, so each item has 4 numbers after dot (i.e. 128.7153). After formating i would move to duplications removal and then deremination of c array's items count. Would you please help me with that part? Thank you in advance. The code so far looks lite this:

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
double m_l[10] = {12.05132, 178.0779, 52.13106, 44.15764, 100.15764, 726.0874, 109.11398, 9.11518, 
198.10388, 36.0773}

vector<double> b;

 for (int d = 0; d < 10; d++){
     for(int e = 0; e < 10; e++){
         for(int f = 0; f < 10; f++){
             for(int g = 0; g < 10; d++){
                 for(int h = 0; h < 10; h++){
                     b.push_back(m_l[d]+m_l[e]+m_l[f]+m_l[g]+m_l[h]);}}}}}



int bSize = sizeof(b)/sizeof(b[0]);
cout << "The size of the b is: " << bSize << "\n";
cout << " " << "\n";

_______________________________________________________
myformattedlist = ["%.4f" % item for item in b]
c = set(myformattedlist)
_______________________________________________________

int cSize = sizeof(c)/sizeof(c[0]);
cout << "The size of the c is: " << cSize << "\n";
cout << " " << "\n";

return 0;
}
Alan
  • 9
  • 3
  • 1
    Your calculations for `bSize` and `cSize` are for C-style arrays. Your `b` (and in future, `c`) are `std::vector`s, which are a lot more like Python lists: there's a length method that does what you want, no sizeof shenanigans required. – Adriaan de Groot Mar 23 '21 at 23:50

1 Answers1

-2

If you just want to remove duplicates inside the vector you can do this:

c++11:
for (auto it = b.begin(); it != b.end(); it++) // first iterate over the vector
    for (auto it2 = it + 1; it2 != b.end(); it2++) // then for each value after the current, check if there is a duplicate
        if (*it2 == *it) // if it is duplicate
        {
            auto tmp = it2; // put the value of the iterator in tmp
            it2--; // decrease the iterator so we don't lose it when erasing (which can cause a segfault)
            b.erase(tmp); // then remove it
        }
c++98:
for (std::vector<double>::iterator it = b.begin(); it != b.end(); it++)
    for (std::vector<double>::iterator it2 = it + 1; it2 != b.end(); it2++)
        if (*it2 == *it)
        {
            std::vector<double>::iterator tmp = it2;
            it2--;
            b.erase(tmp);
        }
c-style:
for (int i = 0; i < b.size(); i++)
    for (int j = i + 1; j < b.size(); j++)
        if (b[j] == b[i])
        {
            b.erase(b.begin() + j);
            j--; // decrease the index so we don't skip one member
        }

And for the size, just use the member method std::vector::size (in your case b.size())

Fayeure
  • 1,181
  • 9
  • 21
  • This assumes `b` is sorted, doesn't it? – m88 Mar 24 '21 at 00:00
  • No, not necessarily, for each member we check for a duplicate in all next members, then remove them so when we go further we won't have a duplicate before, so the vector doesn't need to be sorted for that. – Fayeure Mar 24 '21 at 00:03
  • Oh right, there's a 2nd loop. Alternatively, OP could use `std::set` to only keep unique values if he doesn't explicitly need a `std::vector`. – m88 Mar 24 '21 at 00:10
  • Thank you Fayeure for your help. The only thing that remais, i believe, is formatting. How would i format items in array b (each item with 4 digits after dot (e.g. 128.7153)? – Alan Mar 24 '21 at 00:23
  • If you just want to print them with 4 digits after dot, this post can help you: https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout#:~:text=You%20can%20set%20the%20precision,of%20a%20float%20or%20double. But if you want to format the values themselves with just 4 digits after dot you can just do: `static_cast< double >(static_cast< int >(number * 10000) / 10000.)` (the dot after the second 10000 is important, it specifies that you want to do the operation as double) – Fayeure Mar 24 '21 at 00:31
  • Explanations : you multiply the number by 10000, which puts the 4 first digits after the dot before the dot, then you convert it to an int which deletes the decimal part, then you divide it by 10000 again and convert it back to a double to put your 4 digits back after the dot (EDIT: the `static_cast< double >` is not mandatory, it works aswell without it, so `static_cast< int >(number * 10000) / 10000.` is enough because the `10000.` converts the expression to double) – Fayeure Mar 24 '21 at 00:36
  • Why all those bad votes? Can you give a good answer if my answer is bad? – Fayeure Mar 24 '21 at 13:36