C++ 开始和结束不起作用
c++ begin and end not working
这是代码:
#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))
我已经包含了算法和迭代器。然而,编译器无法编译。请帮忙!我已经尝试过谷歌这个。
在 C++11 中引入了将 std::begin
和 std::end
与传统 C 数组一起使用,因此您需要在编译器调用中添加-std=c++11
。
tyr 来使用它:
#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())
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 获取热线(CIN,答案5);在我的程序结束时不起作用
- 文件结束输入不起作用
- C++ 开始和结束不起作用
- Qt QJsonModel 在网络请求结束时不起作用
- C++映射擦除(开始,结束)不起作用
- 文件结束检查不起作用 (C++)