|
小弟写了一程序,在Windows下由vc,dev-cpp,编译通过,并顺利执行。
到了Linux7.2下,编译通过,但在执行时在执行到某一函数内时,出现Segmentation fault错误。
- void CPreprocess::ProcessSegment()
- {
- int index(0);
- cout << "进入ProcessSegment()" << endl;
- map<string, string>::iterator posMap;
- map<string, string>::iterator endMap = m_mapSegment.end();
- vector<string>::iterator pos = m_vecKeyWordBuf.begin();
- vector<string>::iterator end = m_vecKeyWordBuf.end();
- for( ; pos != end; ++pos)
- {
- int len = pos->length();
- for(int i=0; i<len; i+=2)
- {
- cout << "进入内部循环" << endl;
- cout << "当前i " << i << endl;
- string word = pos->substr(i, 2);
- cout << "word : " << word << endl;
- if((posMap = m_mapSegment.find(word)) != endMap)
- {
- string buf = *pos;
- cout << "buf : " << buf << endl;
- buf.erase(i, 2);
- buf.insert(i, posMap->second);
- cout << buf << buf.size() << endl;
- m_vecKeyWordBuf.push_back(buf);
- m_vecAndClass.push_back(m_vecAndClass[index]);
- }
- }
- ++index;
- }
- cout << "退出ProcessSegment()" << endl;
- }
复制代码
这段代码的功能是将获得一个字符串中可拆分汉字拆分成两个汉字后的字符串(呵呵,比较饶口)
比如“好的”,经过这个函数处理后,就是“好白勺”。
m_mapSegment 是存储(的,白勺)的map<string, string>。
现在的问题是出在这行代码中:
现在的问题是当运行到某一处时,string word = pos->substr(i, 2);这行代码将不能工作,报错segmentation fault。
而这时候i,和i+2都在有效的范围内。
不过将
m_vecAndClass.push_back(m_vecAndClass[index]);
或
string word = pos->substr(i, 2);
任一行注释掉后可以正确运行~~
请高手指点,谢谢~~
我的gcc 版本是2.96. redhat linux 7.2 |
|