hello,
This is my 1st post here!
*welcome drjay*
Thanks! I look answering questions and getting answers to other!
Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:
string = "hello world! hello world... I'm a live"
map<string,int>
hello 2
wold 2
! 1
. 3
Im 1
' 1
a 1
live 1
6
This is how the output should look like when the map is looped/traversed...
This is part of a bigger project... this is how I tackle word and punctuations.
Read the string, call method on the string that removes all punctuations. call a second method on the original string to filter all punctuations.
split the two strings using the space as delimiter/splitter, add to a vector<string> (this vector will obviously have repeats).
call method addtomap(vector ,map)
loop through the vector
check if vector[i] is there in the map
if found second++
else insert(vector[i],1)
the way i have it setup for spaces is i call a method that returns an int, the number of spaces.
create a new string of size 'number of spaces'
string spaceLine;
int noofspaces;
spaceLine.inser t(0, noofspaces, ' ');
basically SpaceLine is a string of spaces.
then i call a method addSpacesToMap( string,map)
loop through the string, check if " " is present in the map
if found second++
else insert(" ",1)
=============== =========error= =============== =============
test1.cpp: In function `void addSpacesToMap( std::string, std::map<std::s tring, int, std::less<std:: string>, std::allocator< std::pair<const std::string, int> > >&)':
test1.cpp:96: error: invalid conversion from `char' to `const char*'
test1.cpp:96: error: initializing argument 1 of `std::basic_str ing<_CharT, _Traits, _Alloc>::basic_ string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc = std::allocator< char>]'
=============== =========error= =============== =============
=============== ==my code =============== =============== ===
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
typedef map<string,int> ::iterator itermap;
string parsingForPunct uations(string line);
int parsingForSpace s(string line);
void addToMap(const vector<string> &word, map<string,int> &map);
void addSpacesToMap( const string &space, map<string,int> &map);
int main(){
map<string,int> mymap;
string str = "Hello! hello world... I'm I'd @ 12+3 (hello)";
//string newstr = parsingForPunct uations(str);
int noofspaces = parsingForSpace s(str);
cout << "str: " <<str<<endl;
//cout << "newstr: " <<newstr<<end l;
cout << "# of spaces: " <<noofspaces<<e ndl;
string spaceLine;
vector<string> tempVec;
tempVec.push_ba ck("hello");
tempVec.push_ba ck("world");
tempVec.push_ba ck("apple");
tempVec.push_ba ck("cat");
tempVec.push_ba ck("hello");
tempVec.push_ba ck("world");
tempVec.push_ba ck(" ");
tempVec.push_ba ck(" ");
spaceLine.inser t(0, noofspaces, ' ');
cout << "Spaces Line:" <<spaceLine<<"e ndl"<<endl;
for(int i = 0 ; i < tempVec.size(); i++){
cout << tempVec[i]<<endl;
}
addToMap(tempVe c,mymap);
addSpacesToMap( spaceLine,mymap );
return 0;
}
string parsingForPunct uations(string line){
string str;
for(int i = 0 ; i<line.size() ; i++){
char j = line[i];
if(j>=33 && j<=47){
str = str + " " +line[i];
}
}
return str;
}
int parsingForSpace s(string line){
int spaces = 0;
for(int i = 0 ; i<line.size() ; i++){
spaces += (line.at(i)==' ');
}
return spaces;
}
void addToMap(const vector<string> &word, map<string,int> &map){
itermap it;
for(int i = 0 ; i < word.size() ; i++){
it = map.find(word[i]);
if(it!=map.end( )){
it->second++;
}else{
map.insert(pair <string,int>(wo rd[i],1));
}
}
}
void addSpacesToMap( string space, map<string,int> &map){
itermap it1;
for(int i = 0 ; i < space.length() ; i++){
it1 = map.find(space[i]);
if(it1!=map.end ()){
it1->second++;
}else{
map.insert(pair <string,int>( " ",1));
}
}
}
=============== =======my code=========== =============== ==
if you comment out the addSpacesToMap( ), program will compile. also this just a segment of my program...
this is probably the longest 'question' here... but if you get to this point and you are reading this, you only need to help me out with one method and one error:
error: invalid conversion from `char' to `const char*'
thanks in advance
drjay
This is my 1st post here!
*welcome drjay*
Thanks! I look answering questions and getting answers to other!
Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:
string = "hello world! hello world... I'm a live"
map<string,int>
hello 2
wold 2
! 1
. 3
Im 1
' 1
a 1
live 1
6
This is how the output should look like when the map is looped/traversed...
This is part of a bigger project... this is how I tackle word and punctuations.
Read the string, call method on the string that removes all punctuations. call a second method on the original string to filter all punctuations.
split the two strings using the space as delimiter/splitter, add to a vector<string> (this vector will obviously have repeats).
call method addtomap(vector ,map)
loop through the vector
check if vector[i] is there in the map
if found second++
else insert(vector[i],1)
the way i have it setup for spaces is i call a method that returns an int, the number of spaces.
create a new string of size 'number of spaces'
string spaceLine;
int noofspaces;
spaceLine.inser t(0, noofspaces, ' ');
basically SpaceLine is a string of spaces.
then i call a method addSpacesToMap( string,map)
loop through the string, check if " " is present in the map
if found second++
else insert(" ",1)
=============== =========error= =============== =============
test1.cpp: In function `void addSpacesToMap( std::string, std::map<std::s tring, int, std::less<std:: string>, std::allocator< std::pair<const std::string, int> > >&)':
test1.cpp:96: error: invalid conversion from `char' to `const char*'
test1.cpp:96: error: initializing argument 1 of `std::basic_str ing<_CharT, _Traits, _Alloc>::basic_ string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc = std::allocator< char>]'
=============== =========error= =============== =============
=============== ==my code =============== =============== ===
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
typedef map<string,int> ::iterator itermap;
string parsingForPunct uations(string line);
int parsingForSpace s(string line);
void addToMap(const vector<string> &word, map<string,int> &map);
void addSpacesToMap( const string &space, map<string,int> &map);
int main(){
map<string,int> mymap;
string str = "Hello! hello world... I'm I'd @ 12+3 (hello)";
//string newstr = parsingForPunct uations(str);
int noofspaces = parsingForSpace s(str);
cout << "str: " <<str<<endl;
//cout << "newstr: " <<newstr<<end l;
cout << "# of spaces: " <<noofspaces<<e ndl;
string spaceLine;
vector<string> tempVec;
tempVec.push_ba ck("hello");
tempVec.push_ba ck("world");
tempVec.push_ba ck("apple");
tempVec.push_ba ck("cat");
tempVec.push_ba ck("hello");
tempVec.push_ba ck("world");
tempVec.push_ba ck(" ");
tempVec.push_ba ck(" ");
spaceLine.inser t(0, noofspaces, ' ');
cout << "Spaces Line:" <<spaceLine<<"e ndl"<<endl;
for(int i = 0 ; i < tempVec.size(); i++){
cout << tempVec[i]<<endl;
}
addToMap(tempVe c,mymap);
addSpacesToMap( spaceLine,mymap );
return 0;
}
string parsingForPunct uations(string line){
string str;
for(int i = 0 ; i<line.size() ; i++){
char j = line[i];
if(j>=33 && j<=47){
str = str + " " +line[i];
}
}
return str;
}
int parsingForSpace s(string line){
int spaces = 0;
for(int i = 0 ; i<line.size() ; i++){
spaces += (line.at(i)==' ');
}
return spaces;
}
void addToMap(const vector<string> &word, map<string,int> &map){
itermap it;
for(int i = 0 ; i < word.size() ; i++){
it = map.find(word[i]);
if(it!=map.end( )){
it->second++;
}else{
map.insert(pair <string,int>(wo rd[i],1));
}
}
}
void addSpacesToMap( string space, map<string,int> &map){
itermap it1;
for(int i = 0 ; i < space.length() ; i++){
it1 = map.find(space[i]);
if(it1!=map.end ()){
it1->second++;
}else{
map.insert(pair <string,int>( " ",1));
}
}
}
=============== =======my code=========== =============== ==
if you comment out the addSpacesToMap( ), program will compile. also this just a segment of my program...
this is probably the longest 'question' here... but if you get to this point and you are reading this, you only need to help me out with one method and one error:
error: invalid conversion from `char' to `const char*'
thanks in advance
drjay
Comment