Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何访问字符串中的数字并将其转换为整数?_C++_Arrays_String_C++11_Procedural Programming - Fatal编程技术网

C++ 如何访问字符串中的数字并将其转换为整数?

C++ 如何访问字符串中的数字并将其转换为整数?,c++,arrays,string,c++11,procedural-programming,C++,Arrays,String,C++11,Procedural Programming,我在这里使用stoi函数,它给出了无效的参数错误 这里,输入文件类似于“S13S12S11S10S1”。我想把数字保存在一个数组中,比如秩[0]=13,秩[1]=12等等 #include <iostream> #include <fstream> #include <sstream> using namespace std; int main() { ifstream fin("input.txt"); string input; fin>>

我在这里使用stoi函数,它给出了无效的参数错误

这里,输入文件类似于“S13S12S11S10S1”。我想把数字保存在一个数组中,比如秩[0]=13,秩[1]=12等等

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
 ifstream fin("input.txt");
 string input;
 fin>>input;

 int count=0;
 int val;
 int rank[4];
 for(int i=0 ; i < input.size(); i++)
 {

    string s1,s2;
    s1=input[i];
    s2=input[i+1];

    if(s1[0]!='S' && s1[0]!='H' &&s1[0]!='D' && s1[0]!='C')
    {
        int a=stoi(s1);
        rank[count]=a;

        if(s2[0]!='S' && s2[0]!='H' &&s2[0]!='D' &&s2[0]!='C')
        {
            int temp;
            int b=stoi(s2);
            rank[count]=10+b;
            count++;
            i++;
        }
        else{
            count++;
        }       
    }


}   

for (int count=0; count<=4 ; count++)
    {
    cout<<rank[count];
    cout<<"\n";
    }

}
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream fin(“input.txt”);
字符串输入;
fin>>输入;
整数计数=0;
int-val;
int秩[4];
对于(int i=0;i对于(int count=0;count,您可以将输入字符串标记化,使用'SHDC'作为分隔符。然后使用atoi将标记转换为整数。如果您的输入文件可能具有不同数量的标记,我将使用向量存储秩值

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    ifstream fin("input.txt");
    string input;
    fin >> input;

    const char *delimiters = "SHDC";
    char *next_token = NULL;
    char *token = strtok_s(const_cast<char*>(input.c_str()), delimiters, &next_token);

    vector<int> values;

    while (token != NULL) {
        values.push_back(atoi(token));
        token = strtok_s(NULL, delimiters, &next_token);
    }

    for (int i = 0; i < values.size(); ++i) {
        cout << values[i] << endl;
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream fin(“input.txt”);
字符串输入;
fin>>输入;
常量字符*分隔符=“SHDC”;
char*next_token=NULL;
char*token=strtok_s(const_cast(input.c_str())、分隔符和next_标记);
向量值;
while(令牌!=NULL){
值。推回(atoi(令牌));
token=strtok_s(NULL、分隔符和next_标记);
}
对于(int i=0;icout
s1
是一个字符串。传递给
stoi
的字符在哪里?