vectors

hi everyone

i ve got another question.
how can I find and then print out vector elements i am looking for. i figure it out with one element. like this
1
2
3
4
5
6
7
8
string x;
		 cout <<"insert x ";   // x is thing i am looking for in vecotr
		 cin>>x;
		  if (find(j.begin(), j.end(),x)!= j.end()) 
		  {
	              ch = find(j.begin(), j.end(),x); // iterator
		       int nPosition ;
			   cout << "npositon is : " <<nPosition;

but what if i have in vector j two times the same name of something for example like this in some kind of loop
1
2
3
4
vector <string> name;
string names;
cin >> names;
name.push_back(names);

and you type mike
then george
then charlie
and then mike again..
and you want to find all mikes. you want to find position where they are in vector and then print them both out.
If you need only to print all mikes then you can write

1
2
3
std:;copy_if( name.begin(), name.end(), 
                    std::ostream_iterator<std::string>( std::cout, "\n" ),
                    std::bind2nd( std::equal_to<std::string>(), names ) );


But the simplest way

1
2
3
4
for ( const auto &s : name )
{
   if ( s == names ) std::cout << s << std:;endl;
}
well i rather would find the position where in vector it is situated.. because i have bunch of other vectors and i need print all of the others out on the exact same position. so i have vector name and than vector lastName and when i have mike smith then george mcdonald and then mike rodgers and i want to find mikes.. mike is situted at name[1] then smith is situated at lastName[1] george name[2] and next mike name[3] and rodgers lastName[3] and i need to find just that position where all of the mikes are situated then i use for loop to print all of the other information of every mike...

hope that it makes sense. :)
Sounds like you should be using a class or structure to hold this information, instead of the parallel vectors.
Last edited on
well i did use class but i also have option to delete those information and it was told to me that you can t delete while using classes and then they said that i should use vectors.. and now you are going to tell me to use classes.. :D i did object array but you can t delete one object from array.. at least that what they told me..
I was recommending using a structure or class to hold the information about the person. For example:

1
2
3
4
5
6
7
8
9
struct Person
{
    std::string fname;
    std::string lname;
    std::string street;
    std::string city;
    std::string state;
    std::string post_code;
};


Then use a vector of this structure.

std::vector<Person> people;

Now all the information is contained in this one vector, you don't need the multiple vectors.

@mirec
Your example doesn't get the index of the element, could be a typo but see this http://ideone.com/Lro1S3

To get all the matching elements use a loop http://ideone.com/q7P6ft
@jlb
yeah you are right and i want to use struct after this will work. i know it sounds weird but i want to learn more than just one way of doing things and i know that there has to be way how to do it. but thx.

@naraku9333
that is exactly what i need. second link is awesome and i am ashamed that i didn t think of this by myself. and about my example. the very first source code i wrote it works i compiled it and it works but it finds only first index of the element so whenever i have more than one it just doesnt work. but it keeps buging me now why i couldn t thing of simple loop. anyway... thank you very much... i appreciate it.. :)
If you want to implement searching, why use vectors?
searching in a vector is an O(n) operation.
Try to use a hash table, and you might be better off.
Topic archived. No new replies allowed.