Hi I keep trying to run this procedure in my program, however it keeps telling me that it is calling a vector index out of bounds and im getting extremely frustrated as to why it is doing this. Any help would be greatly appreciated!
Thanks,
I attached the code for the procedure as well
It is dangerous to have multiple vectors which were running the same index. You always need to assure that both the vectors were filled same time. An easier approach is to have a struct or class which combines the triple song, artist and rating and then have only one vector:
Code:
struct SAR
{
std::string song;
std::string artist;
int rating;
// operator need from std::find
bool operator=(const SAR & sar)
{
// here compare all 3 members and return true if they were equal otherwise false
...
}
};
....
void merge(std::vector<SAR> & sar1, std::vector<SAR> & sar2)
{
for (int i = 0; i < (int)sar2.size(); ++i)
{
// check if sar2[i] not in first vector
if (std::find(sar1.begin(), sar1.end(), sar2[i]) == sar1.end())
{
// now push_back the sar2[i] into sar1
}
}
}
To add to what itsmeandnobodyelse just said, a further advantage to this approach is that you can use the standard algorithms, including merge. If this is homework, the goal is probably to actually write the merge function, but in real-life, you should try to avoid writing these kind of functions.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Thanks everyone I got it to work. And yeah this procedure was part of a larger ipod emulating program I had to write for my class and they wanted to see us actually write the merge function manually. Thanks again
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.