この例に従って、指定された区切り文字で文字列を分割しようとしています: http://msdn.microsoft.com/en-us/library/ftsafwz3(v=VS.90).aspx
私のコードは、Visual C++ 2010 ではエラーなしでコンパイルされますが、実行しようとすると、次のエラー メッセージが表示されます。
Test.exe の 0x773a15de で未処理の例外: 0xC0000005: アクセス違反の読み取り場所 0x00000000。
これが私のコードです:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <regex>
using namespace std;
vector<char *> splitString(char in[])
{
vector<char *> parts(15);
char seps[] = " ,\t\n";
char *next_token1 = NULL;
char *token1 = NULL;
token1 = strtok_s(in, seps, &next_token1);
while ((token1 != NULL))
{
if (token1 != NULL)
{
token1 = strtok_s( NULL, seps, &next_token1);
//printf( " %s\n", token1 );
parts.push_back(token1);
}
}
return parts;
}
int main(int argc, char * argv[])
{
char string1[] =
"A string\tof ,,tokens\nand some more tokens";
vector<char *> parts=splitString(string1);
cout << parts[0] <<endl;
cout << parts[1] <<endl;
return 0;
}
ベクトルの要素を表示しようとするのは違法のようですが、なぜですか?
ベクターの容量は十分である必要があります。
printf( " %s\n", token1 );
while ループでトークンを出力します。