识别字符串中的 C 11 关键字 (string.at() 问题)
Identifying C 11 keywords in Strings (string.at() issues)
我已经成功地在主函数中教授提供的代码片段中搜索了c ++ 11关键字。尽管当打印的关键字被打印时,如果它们包含在其他单词中,就像当字符串片段存在甜甜圈时打印一样。所以我创建了两个 if 语句,用于标识字符串中的下一个字符以及它是否是空格字符,如果是,则继续打印它。但是我在编译但不运行的 x.at(( 语句中遇到了问题。有人知道我做错了什么吗?
// C++11 Keyword Search Program
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
typedef vector<string> string_vector;
// return the C++11 keywords that appear in the code snippet
string_vector find_keywords(const string snippet)
{
const string x=snippet;
int len=x.length();
int back=x.back();
int last_char, first_char;
vector<string> answer;
string keywords[]= {"alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand",
"bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", "compl", "const", "constexpr",
"const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum",
"explicit", "export", "extern", "false", "float", "for","friend", "goto", "if", "inline", "int", "long", "mutable",
"namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected public",
"register", "reinterpret_cast","return","short","signed","sizeof","static","static_assert","static_cast","struct",
"switch", "template", "this","thread_local","throw","true","try","typedef","typeid","typename","union","unsigned",
"using","virtual","void", "volatile","wchar_t","while","xor","xor_eq"};
for(int i=0;i<83;++i)
{
string test=keywords[i];
size_t found = x.find(test);
unsigned int testsize=test.size();
int test1=0,test2=0;
if(found!=string::npos)
{
char a=x.at(found-1);
char b=x.at(found+testsize);
int c=isalpha(a);
int d=isalpha(b);
if (c>=1)
test1=1;
if (d>=1)
test2=1;
}
if(found!=string::npos && test1==0 &&test2==0)
cout<<test<<" ";
}
return answer;
}
int main (int argc, char* const argv[])
{
const string snips[] =
{
"int i = 0;",
"i += 1;",
"return (double) x / y;",
"char foo(char bar);",
"double bar(void) const",
"garbage = fuzz + lint;"
};
const string_vector snippets(snips,snips + 6);
for (auto snippet : snippets)
{
cout << "**Snippet** " << snippet << endl << "**Keywords** ";
for (auto keyword : find_keywords(snippet))
{
cout << ' ' << keyword;
}
cout << endl << endl;
}
return EXIT_SUCCESS;
}
last_char=found+len-1; char2=x.at(last_char);
一旦found > 0
,那么last_char >= x.length()
和你的at()
调用就会尝试越界读取。
另请注意,空格不是唯一可能的标记分隔符。人们经常写break;
、return;
、while(condition)
、f(int[])
或this->member
之类的东西。所有这些都包含一个没有空格包围的关键字。
而且,看起来像关键字的字符串可能会出现在字符串文本中,"like this"
.
相关文章:
- 示例代码中使用分隔符将 std::string 拆分为 std::vector 的范围问题
- 将utf16宽std::wstring转换为utf8窄std::string以获得罕见字符时出现问题
- 面对迭代矢量<矢量<string>>和打印数据的问题
- 检查 std::any 变量是否包含 std::string 时出现问题
- "Error reading character of string" 为哈希动态增长数组的问题 (C++)
- 循环遍历向量<string>并保持每个元素"count"时出现问题
- Catch2 迫使我在异常中添加对 std::string 的强制转换,这会产生其他问题吗?
- 向矢量集添加字符串时出现问题<string><>
- 删除 std::string 上的反斜杠字符时出现问题
- 在使用 std::string 和 std::vector 将值插入 std::map 时遇到问题
- string.length()引发问题
- C++:如果我包含多个头文件,如果两个都有 #include,是否会导致任何问题<string>?
- Null<std::string> 的问题
- 识别字符串中的 C 11 关键字 (string.at() 问题)
- 关于getline(cin,string)的C++快速问题
- 在将字符串从std::string转换为const char *时出现数据丢失问题
- 这个字符数组到std::string的转换有什么问题?
- 在"string" "const unsigned char"和"utf8proc_uint8_t"之间转换的问题
- string substr方法问题
- 使用boost vector和string的奇怪boost program_options问题