2012-03-02 31 views
0

嘿家伙我试图洗牌我的动态数组的内容,它不工作。即时通讯想知道如果有任何建议或链接/资源,可以帮助我。我正在尝试使用std :: randomshuffle,但我的测试正在吐出0而不是正确的数据。c + +洗牌动态数组的内容?

Songs *ptr; 
ptr = new Songs[25]; 

ifstream fin; 
fin.open("input.txt"); 

while (fin.good())     //my input 
{ 
     getline(fin, song[num].title);  
     getline(fin, song[num].artist); 
     fin >> song[num].mem; 
     num++; 
     fin>>ws; 
} 
fin.close(); 

和我的继承人功能我尝试使用randomshuffle

void shuffle (char choice, Songs song[], Songs *ptr, string title, string artist, int mem, int num) 
{ 
    if (choice == '4') 
    { 
     std::random_shuffle(ptr, ptr + num);    //shuffle 
    } 
    for (int i = 0; i<num; i++) //test 
    { 
     cout << ptr[i].title << ptr[i].artist << ptr[i].mem << endl; 
    } 
} 
+0

为什么所有的参数'shuffle()'函数?你只使用'ptr'和'num'。而逻辑是否洗牌也不应该在洗牌功能中完成。 – jrok 2012-03-02 00:09:13

+0

以及那些是我的ptr数组的内容,所以我想我不得不包括那些以及 – gamergirl22 2012-03-02 00:17:28

+0

@ gamergirl22绝对不需要,不应该这样做。之后,你必须考虑如何有效地设计代码:这个洗牌功能的重点在于将歌曲洗牌并打印出来。所以我们需要的是歌曲和歌曲(不是'歌曲'和'ptr')也是多余的。 – stinky472 2012-03-02 00:22:24

回答

2

请考虑以下内容,这是一个更现代的C++方法来解决您的问题。创建流运算符,这样你就不必每次要在阅读的时间来解析手动的对象。

#include <algorithm> 
#include <string> 
#include <iostream> 
#include <fstream> 
#include <vector> 
#include <iterator> 

struct song { 
     std::string title, artist; 
     int mem; 
}; 

std::ostream& operator<<(std::ostream& os, const song& s) { 
     return os << s.title << "\t" << s.artist << "\t" << s.mem; 
} 

std::istream& operator>>(std::istream& is, song& s) { 
     std::getline(is, s.title); 
     std::getline(is, s.artist); 
     return is >> s.mem; 
} 

int main() 
{ 
     std::ifstream file("input.txt"); 

     if(!file.is_open()) return 1; 

     std::vector<song> songs((std::istream_iterator<song>(file)), 
           std::istream_iterator<song>()); 
     std::random_shuffle(songs.begin(), songs.end()); 

     std::copy(songs.begin(), songs.end(), 
        std::ostream_iterator<song>(std::cout, "\n")); 
     return 0; 
} 

编译但UNTESTED ON YOUR文件格式

无向量(但请学习他们)这:

 std::vector<song> songs((std::istream_iterator<song>(file)), 
           std::istream_iterator<song>()); 

可以写为:

const size_t sz=20; 
song songs[sz]; 
for(unsigned i=0; i!=sz && file; ++i) 
    file >> songs[i]; 

和福其余nction调用将像

std::random_shuffle(songs, songs+sz); 

但现在认真学习矢量(然后是其他容器)。数组基本上被认为不适合你的任务,这是一个例子,为什么,如果你有超过20个元素的文件,你会得到一个缓冲区溢出,坏事会发生。

http://en.cppreference.com/w/cpp/container/vector

你也不必明确打开和关闭文件(在大多数情况下,你更容易引入错误),因为RAII的:

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

+0

这看起来非常好,但不幸的是,我还没有学习矢量,所以我不知道如何将它纳入我的函数调用等。大声笑。非常好的工作,我给你投票。 – gamergirl22 2012-03-02 00:49:31

+1

现在学习第二本!认真 – 111111 2012-03-02 00:51:19

+0

@ gamergirl22再看一遍,我已经添加了一个额外的位 – 111111 2012-03-02 00:54:42

3

切勿使用istream::good()istream::eof()作为循环条件。它几乎总是产生bug的代码(因为它在这种情况下做。)

尝试:

while (std::getline(fin, song[num].title) && 
     std::getline(fin, song[num].artist) && 
     fin >> song[num].mem) 
{ 
     num++; 
     fin>>ws; 
} 

由于臭指出,您的洗牌是正确的,但可怕的风格。尝试:

void shuffle (char choice, Songs *ptr, int num) 
{ 
    if (choice == '4') 
    { 
     std::random_shuffle(ptr, ptr + num);    //shuffle 
    } 
    for (int i = 0; i<num; i++) //test 
    { 
     std::cout << ptr[i].title << ptr[i].artist << ptr[i].mem << "\n"; 
    } 
}