问题描述
|
该代码应该构成一个字符串数组,对它们进行随机排序,然后打印该顺序。不幸的是,它在其中一个空格中添加了一个空白行(我认为这是getline的工作)。任何想法如何解决?我尝试设置数组[0] = NULL;它抱怨运营商...
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <string>
#include <cstdlib>
using std::cout;
using std::endl;
using namespace std;
void swap (string &one,string &two)
{
string tmp = one;
one = two;
two = tmp;
}
int rand_loc (int size)
{
return (rand() % size);
}
int main()
{
srand(time(NULL));
int size;
cin >> size;
string *array = new string[size];
//array[0] = NULL ;
for (int x = 0; x < size; x++)
{
getline(cin,array[x]);
}
//for (int x = 0; x < size; x++)
//{
// swap (array[rand_loc(size)],array[rand_loc(size)]);
//}
cout << endl;
for (int x = 0; x < size; x++)
{
//out << array[x] << endl;
int y = x + 1;
cout<<y<<\".\"<<\" \"<<array[x]<<endl;
}
delete[] array;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)