Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 识别字符串中的C 11关键字(string.at()问题)_C++_String_Search_C++11 - Fatal编程技术网

C++ 识别字符串中的C 11关键字(string.at()问题)

C++ 识别字符串中的C 11关键字(string.at()问题),c++,string,search,c++11,C++,String,Search,C++11,我已经成功地在主函数中搜索了教授提供的代码片段中的c++11关键字。虽然当打印关键字时,如果它们包含在其他单词(如do)中,则会在字符串片段存在甜甜圈时打印。因此,我创建了两个if语句,用于标识字符串中的下一个字符,以及它是否是空格字符,如果是,则继续打印它。但是我在编译但不运行的x.at()语句中遇到了一个问题。有人知道我做错了什么吗 // C++11 Keyword Search Program #include <iostream> using std::cout; usin

我已经成功地在主函数中搜索了教授提供的代码片段中的c++11关键字。虽然当打印关键字时,如果它们包含在其他单词(如do)中,则会在字符串片段存在甜甜圈时打印。因此,我创建了两个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;
}
//C++11关键字搜索程序
#包括
使用std::cout;
使用std::endl;
#包括
使用std::string;
#包括
使用std::vector;
typedef向量字符串\ u向量;
//返回代码段中出现的C++11关键字
字符串向量查找关键字(常量字符串片段)
{
常量字符串x=代码段;
int len=x.length();
int back=x.back();
int last_char,first_char;
向量答案;
字符串关键字[]={“alignas”、“alignof”、“and_eq”、“asm”、“auto”、“bitand”,
“bitor”、“bool”、“break”、“case”、“catch”、“char”、“char16\u t”、“char32\u t”、“class”、“compl”、“const”、“constexpr”,
“const_cast”、“continue”、“decltype”、“default”、“delete”、“do”、“double”、“dynamic_cast”、“else”、“enum”,
“显式”、“导出”、“外部”、“false”、“float”、“for”、“friend”、“goto”、“if”、“inline”、“int”、“long”、“mutable”,
“名称空间”、“新建”、“无异常”、“非”、“非相等”、“空ptr”、“运算符”、“或”、“或相等”、“私有”、“受保护的公共”,
“register”、“reinterpret\u cast”、“return”、“short”、“sizeof”、“static”、“static\u assert”、“static\u cast”、“struct”,
“switch”、“template”、“this”、“thread\u local”、“throw”、“true”、“try”、“typedef”、“typeid”、“typename”、“union”、“unsigned”,
“使用”、“虚拟”、“无效”、“易失性”、“wchar\u t”、“while”、“xor”、“xor\u eq”};
对于(int i=0;i=1)
test1=1;
如果(d>=1)
test2=1;
}   
如果(找到!=string::npos&&test1==0&&test2==0)
库特
只要
找到>0
,然后
last\u char>=x.length()
和您的
at()
调用就会尝试读取越界

还要注意,空格不是唯一可能的标记分隔符。一个人经常写一些东西,比如
break,或
返回
,或
while(condition)
,或
f(int[])
,或
此->成员
。所有这些都包含一个不被空格包围的关键字


而且,看起来像关键字的字符串可能出现在字符串文本中,
“像这样”

为什么要使用两个
const string
实例?见鬼,你为什么还要使用
const string
实例呢?这里不允许破坏你自己的帖子。我用这个和你的空间作为唯一的标记delimeter,并彻底翻阅了我的代码。首先,我在字符串的前面和结尾插入空格,以避免出现越界问题,并检查每个实例前后的字符,如果它找到了某个字符,我告诉程序忽略它找到该字符的实例。
last_char=found+len-1;
char2=x.at(last_char);