【问题标题】:working with string arrays in c++在 C++ 中处理字符串数组
【发布时间】:2010-11-14 04:44:31
【问题描述】:

我想创建一个包含 50 个元素的列表,每个元素由四个字符组成。每四个字符字符串应该一个一个地进入一个循环,并检查当前字符串中任意位置的三个字母(o、a、e)之一。取决于这些字母是否被识别,不同的命令被执行

我尝试了一整天,我很沮丧,请帮助我....

【问题讨论】:

  • 发布一些有关您尝试过的代码会有所帮助...
  • 您应该更具体地了解要求。是仅对找到的任何字符做出决定,还是还取决于它是哪个字符?字符串是否可以包含多个字符,然后应该怎么做(是否有特定的组合操作)?
  • 谢谢大家,这是一个很大的帮助!是的,你是对的,我应该更准确地回答我的问题,对不起,我很累和绝望:D

标签: c++ arrays string list parsing


【解决方案1】:
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?
    }
}

【讨论】:

    【解决方案2】:

    你可以这样:

    
    #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;
        }
      }
    
    
    

    【讨论】:

    • const int NumberElements > #define NUM_ELEMENTS
    【解决方案3】:

    你也可以使用一些字符串 STL 函数:

    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
    }
    

    【讨论】:

    • 很高兴使用 STL!您甚至可以将其限制为仅 1 个 find_first_of("aeoAEO") 调用。如果要求中没有提到插入,为什么您更喜欢列表而不是向量?
    • 主题说的是“数组”,而问题文本说的是“列表”。使用整个“aeoAEO”字符串将不允许您提供不同的行为 w.r.t.找到的角色,会吗?
    【解决方案4】:
    #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);
        }
    }
    

    【讨论】:

      【解决方案5】:

      你似乎需要的是

      • 表示 4 字符字符串的结构(例如 char[4] 或 std::string),例如称为“元素”。

      • 由 50 个此类元素组成的数组(例如 std::vector 或播放数组),例如称为“元素容器”

      • 循环遍历 ElementContainer 的每个元素并将其输入处理函数

      • 一个接受该元素的函数,并找到提到的任何一个字符。

      您遇到了哪些问题?

      例子:

       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 );
      

      【讨论】:

        【解决方案6】:

        我是 Qt4 框架的忠实拥护者,特别喜欢开发跨平台解决方案。

            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);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多