C++ for循环:字符串从";0“;至;9“;

C++ for循环:字符串从";0“;至;9“;,c++,string,for-loop,C++,String,For Loop,我想编写一个for循环,将字符串从0传递到9: for (string j = "0"; j < "10"; j++) { } (字符串j=“0”j

我想编写一个for循环,将字符串从0传递到9:

for (string j = "0"; j < "10"; j++) {

}
(字符串j=“0”j<“10”j++)的
{
}
但它不知道运算符++(它得到一个整数(1)而不是字符串“1”)

我想写:
j+=“1”
,但是
j
将是
“01”
,然后是
“011”

p.s.我不想使用
#include
或其他功能。(stoi等)


感谢您的帮助

您可以像
j[0]++
那样将第一个字符增加到下一个ascii值。但这仅适用于“0”-“9”

编辑:只是一个想法,不是完美的代码:对于
0
20

   i = 0;
   (j > "9") && (i==0) ? j[i]++ : (++i, j="10");

您可以像
j[0]+
那样将第一个字符增加到下一个ascii值。但这仅适用于“0”-“9”

编辑:只是一个想法,不是完美的代码:对于
0
20

   i = 0;
   (j > "9") && (i==0) ? j[i]++ : (++i, j="10");
有了您的限制(尽管我不知道在没有
的情况下如何使用
字符串
#include
):

const char*x[]=
{
"0", "1", .... , "10"
}
对于(int i=0;i和您的约束条件(尽管我不知道在没有
的情况下如何使用
string
#include
):

const char*x[]=
{
"0", "1", .... , "10"
}

对于(inti=0;i整数循环,然后手动将其转换为字符串

for(int i=0;i<10;i++)
{
字符串j(1,'0'+i);//仅适用于单个数字
}

使用整数循环,然后手动将其转换为字符串

for(int i=0;i<10;i++)
{
字符串j(1,'0'+i);//仅适用于单个数字
}
不是那样,而是

char x[]={'0',0};
for(; x[0] <= '9'; ++x[0])
{
}
charx[]={'0',0};
对于(;x[0]不是那样,而是

char x[]={'0',0};
for(; x[0] <= '9'; ++x[0])
{
}
charx[]={'0',0};

对于(;x[0],使用整数进行迭代,并将其转换为循环内的字符串,如下所示:

// Your loop counter is of type int
for (int i = 0 ; i != 10 ; i++) {
    // Use string stream to convert it to string
    ostringstream oss;
    oss << i;
    // si is a string representation of i
    string si = oss.str();
    cout << si << endl;
}
//循环计数器的类型为int
对于(int i=0;i!=10;i++){
//使用字符串流将其转换为字符串
ostringstream oss;

oss使用整数进行迭代,并将它们转换为循环中的字符串,如下所示:

// Your loop counter is of type int
for (int i = 0 ; i != 10 ; i++) {
    // Use string stream to convert it to string
    ostringstream oss;
    oss << i;
    // si is a string representation of i
    string si = oss.str();
    cout << si << endl;
}
//循环计数器的类型为int
对于(int i=0;i!=10;i++){
//使用字符串流将其转换为字符串
ostringstream oss;
oss您可以将atoi用于:

#include <stdlib.h> // or #include <cstdlib>

for (int j = atd::atoi("0"); j < std::atoi("10"); j++) {

}
#包括//或#包括
对于(intj=atd::atoi(“0”);j
您可以将atoi用于:

#include <stdlib.h> // or #include <cstdlib>

for (int j = atd::atoi("0"); j < std::atoi("10"); j++) {

}
#包括//或#包括
对于(intj=atd::atoi(“0”);j
你到底能用什么?你能更准确地说明你的意图吗?我有一个大小在0到20之间的篮子(它是一个字符串)。我有一个函数,可以得到一个字符串并打印篮子编号中的所有boall
I
。所以我必须做一些类似的事情:for(int I=0;Iprint(I);但是,
i
必须是字符串,我想使用stoi函数(因为它属于,所以我必须发送一个字符串。我可以使用您能使用的具体内容吗?您能更准确地说明您的意图吗?我有一个大小在0到20之间的篮子(它是字符串)。我有一个函数,可以获取一个字符串并打印篮子编号
I
中的所有文件。因此,我必须执行如下操作:for(int I=0;Iprint(I);但是
I
必须是字符串,我想使用stoi函数(因为它属于,所以我必须发送一个字符串。我可以使用thank you!我如何更改它以处理0到20之间的数字?谢谢!我如何更改它以处理0到20之间的数字?谢谢。有没有办法处理0到20之间的数字?@Alonhmiel是的,当值从
9
10
谢谢。对于0-20之间的数字,有没有办法这样做?@AlonShmiel是的,当值从
9
更改为
10
时,您可以有条件地使用
+=