Solved

A better way to do things.. ?

Posted on 2011-03-12
1
287 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
1 Comment
 
LVL 40

Accepted Solution

by:
evilrix earned 250 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now