C++ 使用strtok拆分C字符串

C++ 使用strtok拆分C字符串,c++,strtok,c-strings,C++,Strtok,C Strings,我正在寻找一种方法,以特定的方式使用strtok从C字符串中提取值。我有一个C字符串,我需要取出一个数字,然后把它转换成一个双精度的。我能够很容易地转换为double,但是我需要它根据请求的“度”只提取一个值。基本上,0度将从字符串中拉出第一个值。由于我使用的循环,我当前拥有的代码将遍历整个C字符串。有没有办法只针对一个特定的值,并让它提取出那个双重值 #include <iostream> #include <string> #include &

我正在寻找一种方法,以特定的方式使用strtok从C字符串中提取值。我有一个C字符串,我需要取出一个数字,然后把它转换成一个双精度的。我能够很容易地转换为double,但是我需要它根据请求的“度”只提取一个值。基本上,0度将从字符串中拉出第一个值。由于我使用的循环,我当前拥有的代码将遍历整个C字符串。有没有办法只针对一个特定的值,并让它提取出那个双重值

    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;

    int main() {

        char str[] = "4.5 3.6 9.12 5.99";
        char * pch;
        double coeffValue;

        for (pch = strtok(str, " "); pch != NULL; pch = strtok(NULL, " "))
        {
            coeffValue = stod(pch);
            cout << coeffValue << endl;
        }
        return 0;
    }
#包括
#包括
#包括
使用名称空间std;
int main(){
char str[]=“4.5 3.6 9.12 5.99”;
char*pch;
双系数值;
对于(pch=strtok(str,“”);pch!=NULL;pch=strtok(NULL,“”)
{
系数=stod(pch);

cout为了简单起见,您正在询问如何将标记器中的第n个元素确定为double。下面是一个建议:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {

    char str[] = "4.5 3.6 9.12 5.99";
    double coeffValue;

    coeffValue = getToken(str, 2);   // get 3rd value (0-based math)
    cout << coeffValue << endl;
    return 0;
}

double getToken(char *values, int n)
{
    char *pch;

    // count iterations/tokens with int i
    for (int i = 0, pch = strtok(values, " "); pch != NULL; i++, pch = strtok(NULL, " "))
    {
        if (i == n)     // is this the Nth value?
            return (stod(pch));
    }

    // error handling needs to be tightened up here.  What if an invalid
    // index is passed?  Or if the string of values contains garbage?  Is 0
    // a valid value?  Perhaps using nan("") or a negative number is better?
    return (0);         // <--- error?
}
#包括
#包括
#包括
使用名称空间std;
int main(){
char str[]=“4.5 3.6 9.12 5.99”;
双系数值;
coeffValue=getToken(str,2);//获取第三个值(基于0的数学)

cout为了简单起见,您正在询问如何将标记器中的第n个元素确定为double。下面是一个建议:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {

    char str[] = "4.5 3.6 9.12 5.99";
    double coeffValue;

    coeffValue = getToken(str, 2);   // get 3rd value (0-based math)
    cout << coeffValue << endl;
    return 0;
}

double getToken(char *values, int n)
{
    char *pch;

    // count iterations/tokens with int i
    for (int i = 0, pch = strtok(values, " "); pch != NULL; i++, pch = strtok(NULL, " "))
    {
        if (i == n)     // is this the Nth value?
            return (stod(pch));
    }

    // error handling needs to be tightened up here.  What if an invalid
    // index is passed?  Or if the string of values contains garbage?  Is 0
    // a valid value?  Perhaps using nan("") or a negative number is better?
    return (0);         // <--- error?
}
#包括
#包括
#包括
使用名称空间std;
int main(){
char str[]=“4.5 3.6 9.12 5.99”;
双系数值;
coeffValue=getToken(str,2);//获取第三个值(基于0的数学)

CUT如果你使用C++,为什么不使用一个代码:STI:<代码>和<代码> STD::String Strue?例如:你能举一个例子吗?我需要使用C字符串。我发现如何使它工作。@ AJN68:谁或什么要求你使用一个“C字符串”为什么你使用C++?为什么你不使用一个<代码> STD::STIs<代码>和代码> STD::String Strue?例如:你能举一个例子吗?我需要使用C字符串。我发现如何使它工作。@ AJN68:谁或什么要求你使用“C字符串”?为什么你使用C++ C++?