?
Solved

A better way to do things.. ?

Posted on 2011-03-12
1
Medium Priority
?
294 Views
Last Modified: 2012-05-11
I have the following code snippet:
"      if ((Indeks == 1) || (Indeks == 4) || (Indeks == 7) || (Indeks == 10) || (Indeks == 13) || (Indeks == 16) || (Indeks == 19) || (Indeks == 22) || (Indeks == 25) || (Indeks == 28) || (Indeks == 31) || (Indeks == 34) ) "

Is there another way to express this, i.e. by checking if the value of Indeks contains one of the following values (1,4,7,10,13,16,19,22,25,28,31,34).

Something like
    if (Indeks) is a member of [1,4,7,10,13,16,19,22,25,28,31,34) -> go further..

Anyone ?
0
Comment
Question by:toyboy61
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 40

Accepted Solution

by:
evilrix earned 1000 total points
ID: 35117104
Efficient in terms of runtime performance but verbose

switch(Indeks)
{
   case 1: case 4: case 7: case 10: case 13: case 16:
   case 19: case 22 case 25: case 28: case 31: case 34:
      // do something
      break;
}

Open in new window


Less efficient in terms of runtime performance but more compact.
http://www.cplusplus.com/reference/algorithm/find/

int i = {1,4,7,10,13,16,19,22,25,28,31,34};
size_t x = sizeof(i)/sizeof(i[0]);
if(i+x != std::find(i, i+x, Indeks))
{
   // do something
}

Open in new window

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question