【问题标题】:Identifying C 11 keywords in Strings (string.at() issues)识别字符串中的 C 11 关键字(string.at() 问题)
【发布时间】:2013-10-07 20:31:26
【问题描述】:

我已经成功在main函数中找到教授提供的代码sn-ps中的c++11关键字。虽然当打印关键字时,如果它们包含在其他单词中,例如当字符串 sn-p 存在甜甜圈时会打印 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;
}

【问题讨论】:

  • 你到底为什么要使用 两个 const string 实例?哎呀,你为什么还要使用const string 实例?
  • 这里不允许破坏自己的帖子。

标签: c++ string search c++11


【解决方案1】:
last_char=found+len-1;
char2=x.at(last_char);

只要found &gt; 0last_char &gt;= x.length() 和您的at() 调用尝试越界读取。

还要注意,空格不是唯一可能的标记分隔符。人们通常会写break;return;while(condition)f(int[])this-&gt;member 之类的东西。所有这些都包含一个没有被空格包围的关键字。

而且,看起来像关键字的字符串可能会出现在字符串文字中,"like this"

【讨论】:

  • 我使用这个和你的空间作为唯一的标记分隔符,并完全重新编写了我的代码。首先,我在字符串的前面和结尾插入空格以避免越界头痛,并检查每个实例前后的字符,如果那是一个字母,我告诉程序忽略它找到它的那个实例.
猜你喜欢
  • 1970-01-01
  • 2012-08-15
  • 2020-12-26
  • 1970-01-01
  • 2017-01-15
  • 2015-07-28
  • 2016-07-25
  • 2010-12-07
  • 1970-01-01
相关资源
最近更新 更多