vector problem

I am a beginner in this and im having problem with a program, i want to make a function that will search for a spicific word in a sentence. Got stuck on the vector thing, Read some about it on the internet but nothing that helped me, Would be cool if someone would like to look at it for me.

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
 #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool search(string sentence, string word,vector<string> &it,vector<string> &myvector){
     
     it=find(myvector.begin(),myvector.end(),word);
     }

int main()
{

    string sentence;
    string word;
        vector<string>myvector (sentence,word);
    vector<string>::iterator it;
    cout<<"type in a sentence"<<endl;
    getline(cin,sentence);
    cout<<"Search for a word"<<endl;
    getline(cin,word);
    search(sentence,word);


	system("PAUSE");
}


This code does not work and can't tell why since im far from a pro at this xD
It is not clear what are you using the vector for? Do you want each word of the sentence to push in the vector?
Last edited on
I assumed it was needed to store all the words in the sentence. I want to type in for example: The wallet was empty. Then type in: wallet. Then i want the program to check if the word wallet does appear in the sentence and type something like: "Yes the word wallet was found".
The following code I think will be compiled successfully.

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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <sstream>
 
int main()
{
        std::cout << "Enter a sentence: ";
 
        std::string sentence;
        std::getline( std::cin, sentence );
 
        std::istringstream is( sentence );
 
        std::vector<std::string> v( 
                ( std::istream_iterator<std::string>( is ) ),
                ( std::istream_iterator<std::string>() ) );
 
        std::cout << "Enter a word to search: ";
                
        std::string word;
        std::cin >> word;
 
        bool found = std::find( v.begin(), v.end(), word ) != v.end();
 
        std::cout << "Word \"" << word << "\" is " << ( found ? "" : "not " )
                      << "found in the sentence\n";
}


EDIT: I updated the code.
Last edited on
Ok i have looked at it now and understand the most of it. The parts of it that i dont understand is:
1. What is the std::istream_iterator rows doing?
2. I get this error trying to compile the code:

26 C:\Dev-Cpp\main.cpp no matching function for call to `find(__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >, __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >, std::string&)'

does this maybe have something to do with some settings in my compiler?
Btw, I am glad you are helping me :)
I think that you didi not copy and paste my code (by the way that was updated) correctly.
I did it right now and it has some kind of problem with row 28. It doesnt seem to understand the find funktion. I have all the includes i need.
closed account (3qX21hU5)
Nice Vlad I like the use of the bool for the find function never thought of using it like that.
@joet12

I did it right now and it has some kind of problem with row 28. It doesnt seem to understand the find funktion. I have all the includes i need.


Please, simply copy and paste, copy and paste and nothing more that to test the program.

What compiler are you using?
Last edited on
@vlad from moscow

I did that and it is still complaining. I am using dev c++
closed account (3qX21hU5)
Ahh you are probably using the very very outdated version of Dev C++ Bloodshed. That IDE and Compiler hasn't been updated for several years so that is probably why you are getting that error. You have a few options.

1 Preferable) If you like Dev C++ for a IDE go over here http://orwelldevcpp.blogspot.com/2013/03/dev-c-541-released.html and download the up to date version of the new Orwell Dev C++.

2) If you don't mind which compiler/IDE you use download either codeblocks or Visual Studios Express.

3 Least Preferable) If for some reason you want to keep the old Dev C++ IDE you can just download any up to date compiler and install it. This is not recommended since the old Dev C++ Bloodshed IDE has many bugs that will never be fixed.
You can check the code on-line at www.ideone.com
Ok. I installed code::blocks and checked the code but it is complaining at the same thing. But i checked the code in www.ideone.com and it says that the code is ok. So im not sure what the problem is.
Topic archived. No new replies allowed.