Solved

A better way to do things.. ?

Posted on 2011-03-12
1
292 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 133
gdb doesn't stop on breakpoint 2 132
What is sub-make ? 2 102
Beginner to Unreal Engine 4 5 117
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

739 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