带有STL迭代器的两个循环

时间:2015-03-26 18:04:52

标签: c++ stl

如何使用STL迭代器重写这个?

for (int i = 0; i < board.size(); i++) {
      for (int j = 0; j < board[i].size(); j++) {
        if (board[i][j] == '*') {
          if (board[i][j + 1] == '*') {
            j++;
            sum += a;
          } else {
            sum += b;
          }
        }
      }
    }

我必须看到这样的东西(但也许这不是一个好主意?):

vector<string>::iterator board_elem;
vector<string>::iterator cell_itr;

for (board_elem = board.begin(); board_elem != board.end(); board_elem++) {
  for (cell_itr = board_elem.begin(); cell_itr != board_elem.end(); cell_itr++) {
    cout << board_elem[cell_itr];
  }
}

5 个答案:

答案 0 :(得分:1)

一般来说,我没有看到原因,为什么你应该在这里使用迭代器,但如果你想:

for (auto bit = board.begin(); bit != board.end(); ++bit) {
    for (auto sit = bit->begin(); sit < bit->end(); ++sit) {
        if (*sit == '*') {
            if (*(++sit) == '*') {
                sum += a;
            } else {
                sum += b;
            }
        }
    }
}

如果你不能保证最后一个字符不是'*',那么你必须用以下代码替换最后一个字符:

if ((cit+1) != it->end()  && *(++cit) == '*') { 

如果你觉得有点乱,你也可以使用基于范围的循环(虽然我不推荐它):

for (auto& e : board) {
    bool isFirst=true; //next * will be the first one in a row
    for (auto& c : e) {
        if (c == '*') {         
            sum += b;
            if (!isFirst) {
                sum = sum - 2*b + a; //second in a row - subtract what we falsely added             
            }   
            isFirst=!isFirst;           
        } else {
            isFirst = true;
        }
    }
}

答案 1 :(得分:0)

如果董事会被定义为

std::vector<std::string> board;

然后你可以使用

std::vector<std::string>::iterator board_elem;
std::string::iterator cell_itr;

for (board_elem = board.begin(); board_elem != board.end(); ++board_elem) 
{
    for (cell_itr = board_elem->begin(); cell_itr != board_elem->end(); ++cell_itr) 
    {
        // your code here using *cell_itr to get the character
    }
}

更好的方法是使用C ++ 11并使用基于范围的for循环:

for (auto& row : board)
{
    for (auto& col : row)
    {
        // your code here using *col to get the character
    }
}

答案 2 :(得分:0)

您可以使用迭代器尝试以下方式:

vector<string>::iterator board_elem; // board is a vector of string
string::iterator cell_itr;

for (board_elem = board.begin(); board_elem != board.end(); ++board_elem) {
  for (cell_itr = board_elem->begin(); cell_itr != board_elem->end(); ++cell_itr) {
    if (*cell_itr=='*') {
        ++cell_itr;
        if (cell_itr != board_elem->end() && *cell_itr=='*') {
            sum += a;
        } else {
            sum += b;
            --cell_itr;
        }
    }
  }
}

答案 3 :(得分:0)

假设board是字符串的向量,那么我将使用std::string的搜索工具。也许是这样的:(未经测试的代码)

for ( auto iter = board.begin(); iter != board.end(); ++iter ) {
    std::string::size_type j = 0;
    while (j < iter->size() - 1) {
        if ((j = iter->find(start, "**")) == std::string::npos)
            break;
        // found a "**" at offset 'j'
        // do something here
        j++;
    }
}

您也可以使用C ++ 11样式的for循环而不是迭代器:

for ( const std::string &str : board ) {
    std::string::size_type j = 0;
    while ( j < str.size() - 1 ) {
        if ((j = str.find(start, "**")) == std::string::npos)
            break;
        // found a "**" at offset 'j'
        // do something here
        j++;
    }
}

我意识到这不是你问题的严格答案(我如何在这里使用迭代器),但是(恕我直言)这是解决问题的更好方法。

答案 4 :(得分:-2)

vector<vector<string> >::iterator board_elem = board.begin();

while (board_elem != board.end()) {
    vector<string>::iterator cell_itr = (*board_elem).begin();
    while (cell_itr != (*board_elem).end()) {
        cout << *cel_itr << '|';
        ++cel_itr;
    }
    cout << endl;
    ++board_elem;
}

正如你所看到的,我制作了一个字符串向量的向量。你写它的方式根本没有任何意义。在您的示例中,板迭代器将返回一个字符串。在我的例子中,电路板的每一行都是一个字符串向量。因此,board迭代器返回一个字符串向量,这些字符串被写出来,由&#39; |&#39;分隔。每行用换行符分隔。