简体   繁体   English

转换矢量 <string> * char到int

[英]Convert vector<string> * char to int

I need help in my following code and hope that you can help me through. 我需要以下代码中的帮助,希望您能帮助我完成。 All I wanted is to pass in INT type to setX() and setY(). 我想要的只是将INT类型传递给setX()和setY()。 However, there is no way for me to convert vector char* to int. 但是,我无法将vector char *转换为int。 Is there alternative to this? 有替代品吗?

template<class T>
vector<string> Delimiter(T inputString){
    int count=0;
    char str[inputString.length()];
    strcpy(str,inputString.c_str());
    char * pch;

    vector<string> returnContainer;

    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str,",[]");
    while (pch != NULL)
    {
        returnContainer.push_back(pch);
        pch = strtok (NULL, " ,[]");
        count++;
    }
    for(int i=0; i<returnContainer.size(); i++){
    cout << "return:" << returnContainer[i] << endl;
    }
    return returnContainer;
}

//Main() //主要()

fileDataAfterFiltered = Delimiter(fileData[i]); // Delimiter (vector<string> type)

point2DObj[point2DCount].setX(fileDataAfterFiltered[1]); // error
point2DObj[point2DCount].setY(fileDataAfterFiltered[2]); // error

//Assn3.cpp:107:59: error: no matching function for call to 'Point2D::setX(std::basic_string&)' //Assn3.cpp:107:59:错误:没有匹配函数来调用'Point2D :: setX(std :: basic_string&)'

there's plenty of ways of converting string to int. 有很多方法可以将字符串转换为int。 boost::lexical_cast is one which will magically do the conversion you want. boost :: lexical_cast是一个神奇地进行你想要的转换的人。 Otherwise you can use atoi (if you don't care about errors), or strtol (if you do). 否则你可以使用atoi(如果你不关心错误),或strtol(如果你这样做)。

point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str()));
point2DObj[point2DCount].setX(boost::lexical_cast<int>(fileDataAfterFiltered[1]));

Delimiter() returns a vector<string> and you give one of these strings to setX() and setY() , but both expect an integer parameter. Delimiter()返回一个vector<string> ,你将其中一个字符串setY() setX()setY() ,但两者都需要一个整数参数。 You must convert the string to int 您必须将字符串转换为int

int x = atoi(fileDataAfterFiltered[1].c_str());
point2DObj[point2DCount].setX(x);
int y = atoi(fileDataAfterFiltered[2].c_str());
point2DObj[point2DCount].setY(y);

But : in C++ array and vector elements start at 0 not 1, so you might want to replace this with fileDataAfterFiltered[0] and fileDataAfterFiltered[1] respectively. 但是 :在C ++数组中,向量元素从0开始而不是 1,因此您可能希望分别用fileDataAfterFiltered[0]fileDataAfterFiltered[1]替换它。

If you are using a C++11 compiler, function std::stoi() will do the trick: 如果您使用的是C ++ 11编译器,函数std::stoi()将起到作用:

point2DObj[point2DCount].setX(std::stoi(fileDataAfterFiltered[1]));

Otherwise you can use the old atoi() : 否则你可以使用旧的atoi()

point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str()));

Aside from this, your code has many other problems, but I hope you can fix them by yourself. 除此之外,您的代码还有许多其他问题,但我希望您可以自己修复它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM