1

I have no experience on C++ but recently need to rewrite a C++ project in Python. I met several problems that I failed to solve them and some of them are below:

Problem 1

Let's say that there's a map sampleMap and an integer anConstant in C++ code:

for (typename map <string, vector <pair <unsigned int, int> > >::iterator l = sampleMap.begin(); l != sampleMap.end(); ) {
    if (l->second.size() < anConstant) {
        typename map <string, vector <pair <unsigned int, int> > >::iterator tmp = l;
        tmp = l; ++tmp; sampleMap.erase (l); l = tmp;
    } else {
        ++l;
    }
} 

In my understanding, what the code means is that a (key, value) pair should be removed from the map sampleMap if the value's (actually a vector) size is smaller then the integer (anConstant).

So I rewrote the code in Python in below:

for key, value in sampleMap.copy().items():
    if len(value) < anConstant:
        del sampleMap[key]

But seems that it didn't work correctly. Maybe I misunderstood what the c++ code said, can anyone kindly help me to understand the c++ code?

Problem 2

Same, let's say there's a map named sampleMap, a vector named sampleVector, two constants named constantOne and constantTwo.

for (typename map <string, vector <pair <unsigned int, int> > >::iterator l = sampleMap.begin(); l != sampleMap.end(); ++l) {
    if (sampleVector.size() - constantOne < constantTwo){
        sampleVector.push_back(make_pair <string, unsigned int> (l->first, l->second.size()));
        sampleVector.erase(sampleVector.end());
    }
}

In my understanding, the code is saying that while looping the map sampleMap if the condition in the if statement is met, then make the (key, value)'s size a new pair and append the pair into sampleVector.

But I don't understand the last sentence: It doesn't seem to try to remove the last element in the vector. So what does it do? The code runs correctly.

Please kindly help me to understand the c++ code. Thank you!

================EDIT==================

Thank you all for the solution!

For problem 1, after tested the c++ code and python code I found that the python code worked well. Anyway I made sure what the c++ code means, I learnt a lot :P

For problem 2, I still don't know what sampleVector.erase(sampleVector.end()) does here, but I tried to rewrite it as del sampleVector[-1] (to delete the last item of sampleVector) here and the output was as same as the one of c++ code. How strange is it! I will open a new post to discuss this problem and will give the new link here.

Again, thank you all! :D

jiaxin
  • 19
  • 3
  • I don't know python at all but `sampleMap.copy().items()` looks like it should be `sampleMap.items()` since you don't want to modify a copy of the map. – NathanOliver Mar 05 '19 at 15:23
  • 2
    I'd recommend you post these as two separate questions. Just cut problem 2 out of this one and change the title, then make a new one for problem 2. SO likes a one question per question kind of format. – Engineero Mar 05 '19 at 15:23
  • Sidenote: Entire first branch of `if`, in the first example, could be simplified to `l = sampleMap.erase (l);` – Algirdas Preidžius Mar 05 '19 at 15:24
  • 1
    Why do you think your python code isn't working? What output are you getting, what are you expecting? What is the input map? Please show a [mcve] – Alan Birtles Mar 05 '19 at 15:31
  • @NathanOliver Because if I modify the original map during iteration then I will get an error :P So I loop the copy one and modify the original one. – jiaxin Mar 05 '19 at 16:01
  • Ah. That makes sense. – NathanOliver Mar 05 '19 at 16:02
  • @Engineero thank you, I will do it tomorrow, it is 1 am in my country now :( Just too sleepy to make a new post – jiaxin Mar 05 '19 at 16:03
  • @AlgirdasPreidžius Oh great! Thank you! The code is very old but also very new to me :P There's a long way for me to learn C++ – jiaxin Mar 05 '19 at 16:14
  • @AlanBirtles Yes you are right, I didn't test the code before I posted it here, I forgot to do it. My bad – jiaxin Mar 05 '19 at 16:17
  • It's hard to distinguish the letter l from the number 1. auto would help this c++ code (probably written before auto was widely supported though). Your python code works (there are multiple ways to do it). On second question, should have been a different question, but sampleVector.end() is NOT the last entry in the vector, but rather the next position after that. I'm not 100% sure if it's UB or a legal +1 at the end. – Kenny Ostrom Mar 05 '19 at 16:56

1 Answers1

0

Problem 1

You can probably just use dictionary comprehension:

sampleMap = {key: value for (key, value) in sampleMap.items() if value[1] > anConstant}

Problem 2

Looks like undefined behavior. In the best case the sampleVector.erase(sampleVector.end()) does nothing.

julians
  • 71
  • 5
  • Thank you! For Problem 2, I tried to delete the last item of the sampleVector in my python code then it ran correctly :O But I agree with you that vector.erase(vector.end()) should be undefined behavior in C++. Anyway I will open a new post for discussing this problem. Thank you for your time to help me! :D – jiaxin Mar 06 '19 at 14:26