c ++开始和结束不起作用

时间:2016-03-11 05:25:04

标签: c++ arrays iterator

以下是代码:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;

int main(int argc, char** argv)
{
  ifstream myfile (argv[1]);
  char ch;
  char operator_symbols[]={'+','-','*','<','>','&','.',
                               '@','/',':','=','~','|','$',
                               '!','#','%','^','_','[',']',
                               '{','}','\"','`','?'
                              };
  while(!myfile.eof())
    {
      myfile.get(ch);
      if(isalpha(ch))
    {
      cout << "isalpha " << ch << endl; 
    }
      else if(isdigit(ch))
    {
      cout << "is num " << ch << endl;
    }
      else if(find(begin(operator_symbols), end(operator_symbols), ch) != end(operator_symbols))
    {
      cout << "is operator sym" << ch << endl;
    }
      else if(ch == '(' || ch == ')' || ch == ';' || ch == ',')
    {
      cout << "is punctuation " << ch << endl;
    }
      else if (isspace(ch))
    {
      cout << "is space " << ch << endl;
    }
    }
}

错误,它与尝试在运算符符号数组中查找字符匹配的if条件有关。 :

****@*****:~/Documents/ABC$ g++ lexer.cpp -o lexer
lexer.cpp: In function ‘int main(int, char**)’:
lexer.cpp:30:42: error: ‘begin’ was not declared in this scope
       else if(find(begin(operator_symbols), end(operator_symbols), ch) != end(operator_symbols))
                                          ^
lexer.cpp:30:65: error: ‘end’ was not declared in this scope
       else if(find(begin(operator_symbols), end(operator_symbols), ch) != end(operator_symbols))

我已经包含了算法和迭代器。然而编译器无法编译。请帮忙!我已经尝试过谷歌了。

2 个答案:

答案 0 :(得分:5)

在C ++ 11中引入了使用std::beginstd::end与传统C数组,因此您需要将-std=c++11添加到编译器调用中。

答案 1 :(得分:-1)

使用这个:

#include <vector>

char str[]={'+','-','*','<','>','&','.',
                           '@','/',':','=','~','|','$',
                           '!','#','%','^','_','[',']',
                           '{','}','\"','`','?'
                            };
vector<char> operator_symbols;
operator_symbols.push_back(str);

if(find(operator_symbols.begin(), operator_symbols.end(), ch) !=        operator_symbols.end())