Cout不会打印我的字符串
Cout will not print my string
在调用permute后,我的程序应该打印添加到我的打印城市字符串中的城市名称,但它只打印空白。
尽管这个程序很难制作,但我没想到我的打印功能会给我带来最烦人的问题。
int main()
{
string cities;
string printCity = "";
string line;
char command = 0;
unsigned city = 0;
while (getline(cin, line))
{
sscanf(line.c_str(), "%c %d", &command, &city);
if (command != 'c')
break;
cities.push_back((unsigned char)city);
printCity +=(city);
}
gFirstCity = cities[0];
unsigned to = 0;
unsigned from = 0;
uint32_t cost = 0;
sscanf(line.c_str(), "%c %d %d %d", &command, &to, &from, &cost);
graph[to][from]=cost;
graph[from][to]=cost;
while (getline(cin, line))
{
sscanf(line.c_str(), "%c %d %d %d", &command, &to, &from, &cost);
graph[to][from]=cost;
graph[from][to]=cost;
}
permute((char*)cities.c_str()+1, 0, cities.length()-1);
cout << "Minimum cost for the tour: ";
cout << printCity;
cout << " is: "<< minTour << endl;
return EXIT_SUCCESS;
}
如果您的城市编号为1、2、3,则printcities
将是一个包含三个字符的字符串,值为' x01' ' x02'
和' x03'
。这印刷不好。如果您试图让printcities保持"123",则需要一个字符串流或std::to_string()。
我同意其他地方所说的:将int插入字符串并不是你想要的方式。相反,首先显式地将city
转换为string
,使用如下内容:
// note: needs <sstream>
string int2str(int x) {
stringstream ss;
ss << x;
return ss.str();
}
然后稍微修改一下你的代码:
printCity += int2str(city);
相关文章:
- 为什么我的字符串变量没有打印完整的字符串?
- 为什么在为其分配编号后无法显示我的字符串值?
- 我的字符串数组一次打印出前两个字符串
- 我的 c 字符串复制函数正在损坏其他变量的堆栈
- 如何修复我的字符串以避免运行时错误?
- 我的字符串类的 += 运算符有问题,它没有正确连接字符串
- c_str() 只读取了我的字符串的一半,为什么?我该如何解决这个问题?是字节问题吗?
- 我不知道为什么我的字符串无法保存值
- if 语句没有比较我的字符串 C++ 中的数值
- 为什么我的字符串在错误的位置打印
- 打印我的字符串阵列时,会遇到细分错误
- 登录系统C++:输入我的字符串User&Pass后,它要求我重新输入它们
- 我从我的字符串函数返回到主函数
- 随机字符串生成器不编辑我的字符串数组 (C++)
- 如何更快地擦除我的字符串
- 我的字符串队列结构有什么问题?
- 我如何显示我的字符串
- 为什么我的字符串令牌在我的C 野牛程序中最终减少时会获得新线
- 我的参数化构造函数说我的字符串变量未定义
- MySQL C ++连接器在我插入数据库时减少了我的字符串