| |

VerySource

 Forgot password?
 Register
Search
View: 842|Reply: 6

Ask this function, why does the while version fail to execute correctly

[Copy link]

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-2-1 12:40:02
| Show all posts |Read mode
void initList (list <string>&slist, const vector <string>&svec)
{
 vector <string> :: const_iterator cit = svec.begin ();

// while (cit! = svec.end ())
// {
// slist.push_front (* cit);
//}

cit = svec.begin ();
for (; cit! = svec.end (); ++ cit)
{
slist.push_back (* cit);
}
}
The program is always in the processing state, and the for version can be executed correctly. It is found that there may be a problem with cit! = Svec.end (), but it is not understood.
Master a lot of advice
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

 Author| Post time: 2020-3-13 15:00:01
| Show all posts
Oh, slist.push_front (* cit); is written as push_back ()
Anyway, in a processing state
Does not execute correctly
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

 Author| Post time: 2020-3-13 16:30:01
| Show all posts
++ cit in the while loop, forgot to write above
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 Unknown

Post time: 2020-3-13 17:00:01
| Show all posts
You have ++ cit in a for loop,
And while cit is not changed in while, of course, it cannot be retreated
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

 Author| Post time: 2020-3-13 18:15:01
| Show all posts
Sweat, confused
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-13 21:45:01
| Show all posts
The while version is different from the for version,
One with push_front and one with push_back
One cit moves incrementally, and one cit does not move.
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-14 03:15:01
| Show all posts
Change to push_back () to exit normally
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list