Stringstream 的麻烦
Stringstream trouble
所以,我正在尝试进行转换(整数到字符串),然后添加此字符串
使用 another.But 似乎 stringstream 不工作..(它通常工作但循环会导致麻烦)
我完成了 google
& 几乎尝试了所有方法,但无法使此代码正常工作..任何人都可以帮助我:(?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
cin>>n;
string arr[n];
string a;
int i=0;
int s;
int c;
cin>>a;
arr[i] = a;
i++;
cout<<"OK"<<endl;
n--;
while(n--)
{
cin>>a;
s = 0;
for(int j=0;j<i;j++)
{
if(arr[j] == a)
{
s = 1;
break;
}
}
i++;
if(s == 0)
{
arr[i] = a;
cout<<"OK"<<endl;
}
else
{
c++;
stringstream ss;
ss<<c;
string m = ss.str();
a+=m;
arr[i] = a;
cout<<a<<endl;
ss.str("");
ss.clear();
}
}
return 0;
}
c
未初始化,需要先初始化,还有s
使用前:
int s = 0;
int c = 0;
数组初始化不能使用非常量变量,考虑写成:
constexpr int MAX_STRINGS = 5;
string arr[MAX_STRINGS];
循环问题在这里:
i++
您将越过最后一个元素的边界。只需将 i++
移动到 while
循环的末尾。
所以,我正在尝试进行转换(整数到字符串),然后添加此字符串 使用 another.But 似乎 stringstream 不工作..(它通常工作但循环会导致麻烦) 我完成了 google & 几乎尝试了所有方法,但无法使此代码正常工作..任何人都可以帮助我:(?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
cin>>n;
string arr[n];
string a;
int i=0;
int s;
int c;
cin>>a;
arr[i] = a;
i++;
cout<<"OK"<<endl;
n--;
while(n--)
{
cin>>a;
s = 0;
for(int j=0;j<i;j++)
{
if(arr[j] == a)
{
s = 1;
break;
}
}
i++;
if(s == 0)
{
arr[i] = a;
cout<<"OK"<<endl;
}
else
{
c++;
stringstream ss;
ss<<c;
string m = ss.str();
a+=m;
arr[i] = a;
cout<<a<<endl;
ss.str("");
ss.clear();
}
}
return 0;
}
c
未初始化,需要先初始化,还有s
使用前:
int s = 0;
int c = 0;
数组初始化不能使用非常量变量,考虑写成:
constexpr int MAX_STRINGS = 5;
string arr[MAX_STRINGS];
循环问题在这里:
i++
您将越过最后一个元素的边界。只需将 i++
移动到 while
循环的末尾。