Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <map>

using namespace std;
static const int16_t Val[10] =
{
	0xC18, 0xC23, 0xC26, 0xC29, 0xC2C, 0xC37, 0xC3A, 0xC3D, 0xC40, 0xC4B,
};
static const int16_t Val_[10] =
{
	0x040, 0x043, 0x046, 0x04A, 0x04D, 0x050, 0x053, 0x056, 0x05A, 0x05D,
};
typedef std::map<std::string,  int16_t>fruit;
static const fruit red={
    {"step1", Val[10]},
    {"step2",Val_[10]},
};
int main()
{
    std::string step_;
    cout<<"enter step"<<endl;
    cin>>step_;
    cout<<"enter number"<<endl;
    cin>>b;
    cout<<red.at(step_)[b];

i want to find the value of array in b.

What I have tried:

cout<<red.at(step_)[b];

is this correct ?
Posted
Updated 8-Dec-20 20:30pm

1 solution

Nope, that is not the correct way to do it (if I got you).
Namely, he type of the map is incorrect, it should store an array of int16_t (instead of a single int16_t). I'm showing you a working example
C++
#include <cstdint>
#include <iostream>
#include <array>
#include <map>

using namespace std;

constexpr size_t ArraySize = 10;
static const array<int16_t, ArraySize> Val =
{
  0xC18, 0xC23, 0xC26, 0xC29, 0xC2C, 0xC37, 0xC3A, 0xC3D, 0xC40, 0xC4B,
};
static const array<int16_t, ArraySize> Val_ =
{
  0x040, 0x043, 0x046, 0x04A, 0x04D, 0x050, 0x053, 0x056, 0x05A, 0x05D,
};

using  fruit = std::map<std::string,  array<int16_t, ArraySize > >;

static const fruit red=
{
    {"step1", Val},
    {"step2",Val_},
};

int main()
{
    std::string step_;
    size_t b;

    cout<<"enter step"<<endl;
    cin>>step_;
    cout<<"enter number"<<endl;
    cin>>b;

    auto it = red.find(step_);
    if ( it != red.end())
    {
      cout << hex << it->second.at(b) << endl;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900