0

ここでは、以下のように文字列トークンを char ポインターにコピーしようとしています。

#include <iostream>
#include <cstring>
#include <string>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace boost;
int main(int, char**)
{
    string text = "token test string";
    char *word;
    char_separator<char> sep(" ");
    tokenizer<char_separator<char>> tokens(text, sep);
    int i=0;
    for (const auto& t : tokens) {
        cout << t << "." << endl;
        word[i] =(const char *)strdup(t); // Error
        i++;    } }

エラーは:test.cpp:18:40: error: cannot convert ‘const std::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘char* strdup(const char*)

4

5 に答える 5

1

ここに直接パスワードを入力してください:

std::ostringstream bfr; 
    word = strtok(& text[0]," ");
        while (word!= NULL) {
                printf("\n Word %s \n",word);
            bfr << word << " ";
            word = strtok(NULL, " ");
            j++; 
            }    
于 2013-09-08T07:13:43.927 に答える
1

文字列に対して c_str を呼び出します。

word[i] = (const char *)strdup(t.c_str());

参考: http ://en.cppreference.com/w/cpp/string/basic_string/c_str

于 2013-09-07T18:44:52.947 に答える
1

これは役立つかもしれません:

int main()
{
    string text = "token test string";
    char_separator<char> sep(" ");
    tokenizer<char_separator<char>> tokens(text, sep);
    std::vector<std::string> > words;
    for (const auto& t : tokens) {
        cout << t << "." << endl;
        words.push_back(t);
    }
    return 0;
}
于 2013-09-07T19:38:56.327 に答える