list<string>::iterator issues

okay so my issue is that I want the user to enter in variables, which get added to list<string> Listings; . I then want the user to be able to search specifically for some thing that they added to the list, display what they searched for if it exists and then display the next five elements in the list.

I understand how to iterate through and display the entire contents of the list<string>

I understand how to search for a specific phrase or sentence etc.. using the std::find() or std::find_if()

What I need to know is how do I search for some thing and if it exists display not only it but say for example the next five variables that are stored in the list<string>.

The below code isn't the best but if I were to give you all my code you'd be here for ours lol... The basics are still the same though

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "string" 
#include "list" 
#include "iostream" 
#include "sstream" 
#include "iterator" 
using namespace std; 

int main() 
{ 
string Name = ""; 
string Listing = " Listing: ";  
list<string> Listings;
string UserChoice;

cout << " Please enter a name for your listing " << endl; 
   cin.ignore();
       getline(cin, Name);
           Listings.push_back(Listing);
               Listings.push_back(Name);
                 /* The user has entered a string which is then stored in  
                    list<string> Listings; along with a default variable called 
                    Listings */ 

// I could do some thing like the following but it doesn't do what I need it to 

for(list<string>::iterator s = Listings.begin(); s != Listings.end(); s++)
   {
      cout << " " << *s; 
   }
                   
// I'm thinking about some thing like this 

 if (s = find(Listings.begin(), s != Listings.end(), UserChoice));
 cout << UserChoice << endl;  

  return 0;
} 


Shouldn't I be able to do some thing like *S+=1; to move it to the next variable in the list, and then continue that process while displaying each item?
Last edited on
You can call the iterator's postfix operator for certain. Also line 33 is not what you want:
1
2
3
4
if((s = find(Listings.begin(),Listings.end(),UserChoice)) != Listings.end()) // we found UserChoice
{
  ...
}



Yeah I just my mistake on that lol, I've tried so many different ways of doing this and I'm just missing some thing some where. The end result of how I want my information to output would be this.

Example Output 1

Example Output 2

Example Output 3

Example Output 4

Example Output 5

(I want them to be displayed at the same time)

I think the issue that I'm having is occurring when I try to move past the initial found variable.

How would I go about that, some thing like the following?
1
2
3
4
5
6
7
8
9
 
      if((s = find(Listings.begin(),Listings.end(),UserChoice)) != Listings.end()) 
       {
           cout << UserChoice << endl; 
             *S += 1;   /* I'd say ++S but i may not nessicarily just move one space I may move a few variables ahead */
                   cout << *S << endl; // Or would it be UserChoice still?
                       etc....  
        }


I've never tried to display multiple at the same time this way and I jsut can't figure it out lol

EDIT: So I was able to move the iterator forward and display the next item in Listings, sadly it removed the previous variable that was displayed.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>

int main()
{
  std::list<std::string> sList;

  sList.push_back("Uno");
  sList.push_back("Dos");
  sList.push_back("Tres");
  sList.push_back("Quatro");
  sList.push_back("Cinco");
  sList.push_back("Seis");
  sList.push_back("Siete");
  sList.push_back("Ocho");
  sList.push_back("Nueve");
  sList.push_back("Diez");

  std::list<std::string>::iterator iter;
  std::string findString = "Cinco";

  if((iter = std::find(sList.begin(),sList.end(),findString)) != sList.end())
  {
    for(int idx = 0; idx < 5; ++idx)
    {
      std::cout << *iter << " ";
      ++iter;
    }
    std::cout << std::endl;
  }

  return 0;
}


Cinco Seis Siete Ocho Nueve
Oh wow I really over complicated it, thanks :)!
Note that the code doesn't handle situations where the matched element is one of the last 5 elements in the list.

How do you want your program to behave if more than one element in the list matches the search term?
Well if more then one matched I'd want them to display in the proper order so say the display would look like.

Matched String (First occurrence of the found string)

Output 1

Output 2

Output 3

Output 4

Matched String Occurrence two

etc..
Topic archived. No new replies allowed.