views:

585

answers:

6

I wanna create a list of 50 elements which consist of four chars each. Every four char string should go into a loop one by one and get checked for one of three letters (o, a, e) anywhere in the current string. dependent on whether or not these letters are identified different commands are executed

I tried all day im frustrated please help me....

+3  A: 
typedef std::list<std::string> MyList;

MyList myList = getMyList();

MyList::const_iterator i = myList.begin(), iEnd = myList.end();

for (; i != iEnd; ++i) {
    const std::string& fourChars = *i;
    if (fourChars.length() == 4) {
        std::string::const_iterator j = fourChars.begin(), 
                                    jEnd = fourChars.end();
        for (; j != jEnd; ++j) {
            char c = *j;
            switch (c) {
                case 'o': case 'O': doO(); break;
                case 'a': case 'A': doA(); break;
                case 'e': case 'E': doE(); break;
                default: // not oae
            }
        }
    }
    else {
        // not 4 chars, what should we do?
    }
}
Pukku
+1  A: 

you can go as:


#define NUM_ELEMENTS 50
#define WIDTH 4

// your function
char list[NUM_ELEMENTS][WIDTH];
//initialize list
for(i=0 ; i < NUM_ELEMENTS ; i++ )
 for(j=0 ; j < WIDTH ;j++)
  {
   switch(list[i][j])
    {
     case 'o': // execute command
             break;

     case 'a': // execute command
             break;

     case 'e': // execute command
             break;
    }
  }


Neeraj
Sharp and concise, and gets things done. I like it.
xtofl
`const int NumberElements` > `#define NUM_ELEMENTS`
GMan
A: 
#include <list>
#include <string> 
#include <boost/foreach.hpp>

using std::list;
using std::string

...

list<string> strings;

// Populate list here

BOOST_FOREACH(string s, strings) {
    bool hasOAE = false;
    BOOST_FOREACH(char c, s) {
        if(c == 'o' || c == 'a' || c == 'e') {
            hasOAE = true;
        }
    }
    if(hasOAE) {
        doSomethingWith(s);
    } else {
        doSomethingElseWith(s);
    }
}
Zifre
+1  A: 

You could also use some string STL function:

list<const string> theStringList; 
// fill list somehow
for(list<const string>::iterator it = theStringList.begin();
 it != theStringList.end();
 ++it) {
  const string& aString = *it;
  // assuming lower-case letters only
  if (aString.find_first_of("a",0) != string::npos) 
   doAStuff();
  else if (aString.find_first_of("e",0) != string::npos) 
   doEStuff();
  else if (aString.find_first_of("o",0) != string::npos) 
   doOStuff();
  else 
   ;// error handling or no-op
}
msiemeri
Nice to use the STL! You could even limit this to just 1 find_first_of("aeoAEO") call. Why would you prefer list over vector if insertion isn't mentioned in the requirement?
xtofl
While the topic said "array", the question text said "list". Using the whole "aeoAEO" string would not allow you to provide different behaviour w.r.t. the character found, would it?
msiemeri
A: 

What you seem to need is

  • a structure representing a 4-char string (e.g. char[4] or std::string), e.g. called "Element".

  • an array of 50 elements of this type (e.g. std::vector or a play array), e.g. called "ElementContainer"

  • a loop iterating over each Element of the ElementContainer and feeding it into a processing function

  • a function accepting that Element, and finding either of the mentioned characters.

Which of these are you having trouble with?

Example:

 typedef char[4] Element1;
 typedef std::string Element2;

 struct Element3 { // slightly OO
     char[4] chars;
     bool has( char c ) const { 
       return std::find( chars, chars+4, c ) != chars+4; 
     }
 };


// a container
Element3 elements[] = {
"adda", "bebe", "xxpo", ...
};
Element3* afterlast = elements + sizeof(elements)/sizeof(elements[0]);


// a function:
void dispatch( Element3& element ) {
  if( element.has( 'a' ) ) return do_a();
  if( element.has( 'e' ) ) return do_e();
  if( element.has( 'o' ) ) return do_o();
}

//iteration
std::for_each( elements, afterlast, &dispatch );
xtofl
A: 

I'm a huge fan of the Qt4 framework, specially for working on cross-platform solutions.

    QStringList list;
list << "abcd" << "zoeb" << "dbca" << "xedz" << "zbco" << "zzzz";
foreach (QString str, list) {
 if(str.indexOf("a", Qt::CaseInsensitive) != -1) {
  do_a(str);
 } else if(str.indexOf("e", Qt::CaseInsensitive) != -1) {
  do_e(str);
 } else if(str.indexOf("o", Qt::CaseInsensitive) != -1) {
  do_o(str);
 }
}
OneOfOne