strtok函数错误

求婚

在我的代码中,unhandled expression error当我使用解析功能时,我得到了一个

在我的PopStack函数中,这是删除vector的最后一个元素的正确方法。

错误是:

Boost_Test.exe中0x0f463b50(msvcr100d.dll)的未处理异常:0xC0000005:访问冲突写入位置0x00345d49。


class Stack
{
public:
    Stack() {GlobalIndex=0; };
    std::vector<char*> v;
    int GlobalIndex;
    void AddStack(char* txt);
    void Parse();
    void PopStack();
    void PrintStack();

};

void Stack::Parse()
{
    char* tok;
    tok = strtok(v[GlobalIndex-1], ";");

    while(tok!=NULL)
     {
        cout<<"\nThe time value is = "<<tok<<endl;
        tok = strtok(NULL, " ");
     }
}




void Stack::AddStack(char* txt)
{

v.push_back(txt);
GlobalIndex++;

}


 void Stack::PopStack()
  {
   v.pop_back();
   GlobalIndex--;
  }

 void Stack::PrintStack()
 {
 std::cout<<v[GlobalIndex-1]<<endl;
 }


int _tmain(int argc, _TCHAR* argv[])
{
 int i;
 Stack s;
 s.AddStack("aaa;1.2");
 s.AddStack("bbb;1.7;");
 s.AddStack("ccc;2.2");
 s.Parse();  // This gives a unhandled expression error 
 s.PopStack(); 
 s.PrintStack();
 return 0;
}
求婚

正如其他成员所建议的那样,我现在已经将c ++字符串与boost库一起使用了。

#include "stdafx.h"
#include <iostream>
#include <asio.hpp>
#include <regex.hpp>
#include <algorithm/string/regex.hpp>
#include <string>
#include <algorithm/string.hpp>

using namespace std;

class Stack
 {

    public:
    Stack() {GlobalIndex=0; };
    std::vector<std::string> v;
    string s;
    int GlobalIndex;
    void AddStack(std::string);
    void Parse();
    void PopStack();
    void PrintStack();

};

void Stack::Parse()
 {

   std::vector<std::string> result;
   boost::split(result,v[GlobalIndex-1],boost::is_any_of(";"));
   cout<<"\nThe boost split is = "<<result[1]<<endl;
}

void Stack::AddStack(std::string txt)
{

v.push_back(txt);
GlobalIndex++;

}

void Stack::PopStack()
{
v.pop_back();
cout<<v.size()<<endl;
GlobalIndex--;

}

void Stack::PrintStack()
{
std::cout<<v[GlobalIndex-1]<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
 {
int i;
Stack s;
s.AddStack("aaaaaa;1.2");
s.AddStack("bbbbb;1.7;");
s.AddStack("ccccc;2.2");
s.Parse();
s.PopStack();
s.PopStack();
s.PrintStack();
cin>>i;
return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章