Map accessing

What is the equivalent cpp code: How to find the previous element and the current element value in the map while iterating.


1
2
3
4
5
6
 for index in range(1,len(_dict)):
     if _dict["width"][index-1] ^ _dict["width"][index] == 1:
          pass
     elif  _dict["width"][index-1] ^ _dict["width"][index] == 0:
          pass
 
not sure what language this is.
if ^ is xor, it is also xor in c++.
c++ has if(condition) else blocks.
c++ is zero indexed, so first array location is 0, not 1 (seems true for yours too)
is this a one-d array or a 2-d array where you only looking at 1 column?

with some assumptions as to what gibberish that code is in, something like
1
2
3
4
5
6
7
8
9
10
11
vector<something> _dict; 
... //fill in _dict with something
for(index = 1; index < _dict.size(); index ++)
{
    if(_dict[index-1] ^ _dict[index] == 1)
       code;
    else //possible second if statement here if necessary. unclear if necessary. 
     {  //begin block
     othercode;  
      } //end block
}


if dict is 2-d, it may be
vector < vector < something> > _dict; //2-d vector
c++ does not allow the above to be different types; you can't have a string by int 2-d thing. you would need something else to do that, if you need it, ask?
if pass means don't do anything this loop, or is some other weird keyword, we have break and continue, which may be what you want, or you can jumble if-blocks around in the loop to do whatever it is most likely
Last edited on
Thank you @jonnin
Looks like Python. Here's a roughly equivalent C++ block. Using a map here gives you the indexing with a string behavior like your Python code (the _dict["width"] part). A single semicolon by itself functions as a no-op like Python's "pass" statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <map>
#include <vector>

using std::map;
using std::vector;
using std::string;

int main()
{
    map<string, vector<int> > _dict;

    // fill up _dict...

    for (int index = 1; index < _dict.size(); index++)
    {
        if ((_dict["width"][index - 1] ^ _dict["width"][index]) == 1)
        {
            ;
        }
        else if ((_dict["width"][index - 1] ^ _dict["width"][index]) == 0)
        {
            ;
        }
    }

    return 0;
}
I see.
in c++ you would not generally DO that, though. you would perhaps say
1
2
3
4
 if ((_dict["width"][index - 1] ^ _dict["width"][index]) >1)
  {
   *actually do something here*
  }

and its understood that if the condition is not true, nothing will happen, you don't need to explicitly say 'do nothing' (though you surely can, as above)
Be careful about using leading underscores for your variable/function names, in many cases, like this one, a leading underscore is reserved for the implementation.

1
2
3
 for index in range(1,len(_dict)):
     if _dict["width"][index-1] ^ _dict["width"][index] == 1:
          pass


1
2
3
4
5
    map<string, vector<int> > dict;

    for (int index = 1; index < dict.size(); index++)
    {
        if ((dict["width"][index - 1] ^ dict["width"][index]) == 1)

I see apples vs oranges on both of these, which is prone to cause out-of-range error.

What does len(_dict) actually return? The _dict is a "dictionary"?
A list of (key,value) pairs? Isn't the len(_dict) the count of pairs in the list?

However, the loop is interested in only one pair, whose key is "width".
What does the count of pairs have to do with the value of one pair?

Does the disctionary even contain a pair, whose key is "width"?

That should be the first question: Do we have width?
http://www.cplusplus.com/reference/map/map/find/
1
2
3
4
5
6
7
8
9
10
11
12
13
map<string, vector<int> > dict;
// fill dict

auto width = dict.find( "width" );
if ( width != dict.end() )
{ // the dict has key "width"

  const auto& values = width->second; // the vector mapped to "width"

  // iterate over vector "values":
  for (int index = 1; index < values.size(); index++)
  {
    if ( (values[index - 1] ^ values[index]) == 1)

Last edited on
Be careful about using leading underscores for your variable/function names, in many cases, like this one, a leading underscore is reserved for the implementation.
Underscore followed by capital letter or 2x consecutive underscores anywhere is what is not allowed.
Not that I'm for such decoration, I think it just makes the code harder to read.
Last edited on
Underscore followed by capital letter or 2x consecutive underscores anywhere is what is not allowed.

Actually even a single underscore is reserved in many places as well.

From the C++20 draft standard N5861 dated April 1, 2020
3 In addition, some identifiers are reserved for use by C ++ implementations and shall not be used otherwise; no
diagnostic is required.
(3.1) — Each identifier that contains a double underscore __ or begins with an underscore followed by an
uppercase letter is reserved to the implementation for any use.
(3.2) — Each identifier that begins with an underscore is reserved to the implementation for use as a name in
the global namespace.


And in this case the variable in question is being used/defined in the global namespace, therefore it is reserved for the implementation.

I also agree that using underscores (either leading or trailing) are hard to read and I avoid using either.

Topic archived. No new replies allowed.