Code:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
typedef std::vector <std::string> str;
int main()
{
str s;
s.push_back("abcd");
s.push_back("efgh");
s.push_back("ijkl");
s.push_back("mnop");
s.push_back("123");
string s1="ijkl"; // Also unnecessary comment, use a better name
std::vector<std::string>::iterator p;
for(p = s.begin(); p!=s.end();p++)
{
if( s1==*p) cout<<"string has been found"<<endl; // Unnecessary comment
}
int n = atoi("123"); // Yes, it is possible. :-)
cout<<n<<endl;
}
The more obvious errors are:
>typedef std::<vector std::string> str;
Misplaced opening angle bracket. It should be:
Code:
typedef std::vector <std::string> str;
>str s("abcd","efgh","ijkl","mnop","123"); // string initialized
Sadly, you cannot initialize a vector like this.
>std::vector<std::string> iterator p ;
iterator is a data type of the vector class, use the scope resolution operator to access it:
Code:
std::vector<std::string>::iterator p;
>for(iterator p = s.begin(); p!=s.end();p++)
p already exists, you're trying to declare it again with an unknown type. This should be:
Code:
for(p = s.begin(); p!=s.end();p++)
>if( s1==s[p])
An iterator is like a pointer, not like an index. s[p] is an undefined operation. Instead, dereference the iterator like a pointer:
>int n = atoi("123"); // is it possible in C++ ?
Yes it is possible, but calling atoi on a string literal directly serves no purpose. It would be easier and faster to simply do this: