bumping it for myself...it was a few pages behind
Printable View
bumping it for myself...it was a few pages behind
when calling ordered second time
start == "2"
so ordered skips "1"
outputs "2" and calls rest
rest starts with "1" and because it is != "2" - outputs it, then skips "2", outpupts "3" and "4"
If you think this behavior is incorrect - fix your code logic
Not really understand what you are trying to achive with this long code, but the desired output could be received simplier
Code:#include <iostream>
#include <list>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::cin;
using std::list;
void ordered(list<string>::iterator& start, std::list<std::string>& phrase);
int main(){
//asks for user input
cout << "Please type in your input: ";
//the container
list<string> sentence;
string x;
//adds user input to container
while(cin >> x)
{
sentence.push_back(x);
}
sentence.sort();
//starts container from beginning, and continues until the end
for(list<string>::iterator iter = sentence.begin(); iter != sentence.end(); iter++)
{
ordered(iter, sentence);
cout << endl;
}
return 0;
}
void ordered(list<string>::iterator& start, std::list<std::string>& phrase)
{
//current word the iterator is on
list<string>::iterator current(start);
cout << *current << ' ';
current++;
if (current == phrase.end())
{
current = phrase.begin();
}
//while the count is not the end
while(current != start)
{
cout << *current << ' ';
current++;
if (current == phrase.end())
{
current = phrase.begin();
}
}
}
May I ask what does this part do?Code:list<string>::iterator current(start);
I also got my code to work, but I am trying to understand yours also.
I had to add a line of code to rline to let it go through the list until it reaches start and then continue from there.Code:void rest(std::string& start, std::list<std::string>& phrase){
//counts the number of words outputted
list<string>::iterator count = phrase.begin();
//current word the iterator is on
list<string>::iterator current = phrase.begin();
while(*current != start){
current++;
}
//while the count is not the end
while(count != phrase.end()){
//if it reaches the end set it to the beginning
if (current == phrase.end()){
current = phrase.begin();
}
//if start equals current continue along and increment current and count
if(start == *current){
current++;
count++;
}
//otherwise output the current and increment current and count
else{
cout << *current << ' ';
current++;
count++;
}
}
}
Seeing that you managed to implement the rotation on your own, I would now suggest that you use say, std::rotate_copy(). For example:Quote:
Originally Posted by dnguyen1022
Code:#include <string>
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
using namespace std;
string word;
cout << "Please enter the word: ";
cin >> word;
list<string> phrase;
cout << "Please enter the phrase: ";
copy(istream_iterator<string>(cin), istream_iterator<string>(),
back_inserter(phrase));
list<string>::iterator found = find(phrase.begin(), phrase.end(), word);
if (found != phrase.end())
{
rotate_copy(phrase.begin(), found, phrase.end(),
ostream_iterator<string>(cout, " "));
}
else
{
copy(phrase.begin(), phrase.end(), ostream_iterator<string>(cout, " "));
}
cout << endl;
}
Ah I see the code makes sense now, and it is muchh shorter! Thank you.
@laserlight
When I get home I'll look over this code and understand the functions, and if I have any questions I'll post back. Thanks!
It was only one day..when I bumped it...