也许周期出现了错误?
for (; it != reg_end; ++it)
代码应该读取文本并查找具有相同开头和结尾字母的单词数(例如,“runner”,“streets”等)。用于数据的文本位于同一文件夹中。代码在构建时成功但在没有调试的情况下运行失败,出现错误......这是完整的代码:
#include <fstream>
#include <iostream> // first error was here
#include <iomanip>
#include <string>
#include <regex>
using namespace std;
const string Cdfv = "Data.txt";
//------------------------------------------------------------
/** Class for counting words with the same begin
and end letters.
Inherits string. */
class row: public string {
public:
int equalBE(regex & re);
};
//------------------------------------------------------------
/** Finds and returns a number in a row of words, which begin
and end with the same letter. */
int row::equalBE(regex & re)
{
int equalQuantity = 0;
string word;
sregex_token_iterator it(begin(), end(), re, -1);
sregex_token_iterator reg_end;
for (; it != reg_end; ++it) {
word = it->str(); // second error was here, there was no ; mark
cout << word << endl;
if (word[0] == word[word.length()-1])
equalQuantity++;
}
return equalQuantity;
}
//------------------------------------------------------------
int AnalyseText(string dfv, regex & re);
//------------------------------------------------------------
int main()
{
string punct = "[\\s,.;:-!?()]+"; // punctuation marks between words in a row
regex re(punct);
cout << "Number of words: " << AnalyseText(Cdfv, re) << endl;
return 0;
}
//------------------------------------------------------------
/** Counts and return number of words in a text,
which begin and end with the same letter */
int AnalyseText(string dfv, regex & re)
{
int equalQuantity = 0;
ifstream fd(dfv.c_str());
row rw;
while (!fd.eof()) {
getline(fd, rw);
equalQuantity += rw.equalBE(re);
}
fd.close();
return equalQuantity;
}
//------------------------------------------------------------
编辑:这是我得到的错误:
Microsoft Visual C ++运行时库
调试错误!
程序:... klaidos pavyzdy \ Zodziu_isskyrimas \ Debug \ Zodziu_isskyrimas.exe
R6010
- abort()被称为
(按“重试”调试应用程序)
中止重试忽略
编辑:这是我在“调用堆栈”窗口中获得的内容:
KernelBase.dll!761ec42d() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]
> msvcr110d.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 152 C++
msvcp110d.dll!std::_Xregex_error(std::regex_constants::error_type _Code) Line 50 C++
Zodziu_isskyrimas.exe!std::_Parser<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char> >::_Error(std::regex_constants::error_type _Code) Line 4757 C++
Zodziu_isskyrimas.exe!std::_Parser<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char> >::_ClassRanges() Line 5093 C++
Zodziu_isskyrimas.exe!std::_Parser<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char> >::_CharacterClass() Line 5117 C++
Zodziu_isskyrimas.exe!std::_Parser<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char> >::_Alternative() Line 5420 C++
Zodziu_isskyrimas.exe!std::_Parser<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char> >::_Disjunction() Line 5466 C++
Zodziu_isskyrimas.exe!std::_Parser<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char> >::_Compile() Line 5498 C++
Zodziu_isskyrimas.exe!std::basic_regex<char,std::regex_traits<char> >::_Reset<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > > >(std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > > _First, std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > > _Last, std::regex_constants::syntax_option_type _Flags, std::forward_iterator_tag __formal) Line 2515 C++
Zodziu_isskyrimas.exe!std::basic_regex<char,std::regex_traits<char> >::basic_regex<char,std::regex_traits<char> ><std::char_traits<char>,std::allocator<char> >(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Str, std::regex_constants::syntax_option_type _Flags) Line 2292 C++
Zodziu_isskyrimas.exe!main() Line 42 C++
Zodziu_isskyrimas.exe!__tmainCRTStartup() Line 536 C
Zodziu_isskyrimas.exe!mainCRTStartup() Line 377 C
kernel32.dll!772a338a() Unknown
ntdll.dll!77eb9f72() Unknown
ntdll.dll!77eb9f45() Unknown
答案 0 :(得分:0)
如果您查看for循环,则it
无法在任何地方启动表单。它应该有一个起点,例如it = 0
或其他。
for (; it != reg_end; ++it) { // The prog. is failing dying for this
word = it->str();
cout << word << endl;
if (word[0] == word[word.length()-1])
equalQuantity++;
}
此外,如果您的reg_end
未正确初始化(仅因某人的评论而被注意到),则可能导致&#34;未定义的行为&#34;这可能会导致问题。好像你正在开始一个for循环来自&#34; Somewhere&#34;并运行它直到&#34;某些东西不满意&#34;每增加一步......
答案 1 :(得分:0)
这是因为字符串
"[\\s,.;:-!?()]+"
不是ECMAScript syntax中的有效正则表达式。所以代码:
string punct = "[\\s,.;:-!?()]+"; // punctuation marks between words in a row
regex re(punct);
抛出一个std :: regex_error,而没有代码捕获它,所以它崩溃了。它与调试或发布版本无关。
类型regex是basic_regex,默认情况下,它使用ECMAScript语法构造正则表达式。如果您只是删除&#34;!?&#34;在字符串中,它修复了错误。