By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

adding # of spaces in string to map

P: 19
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.insert(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::string, 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_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<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 parsingForPunctuations(string line);
int parsingForSpaces(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 = parsingForPunctuations(str);
int noofspaces = parsingForSpaces(str);
cout << "str: " <<str<<endl;
//cout << "newstr: " <<newstr<<endl;
cout << "# of spaces: " <<noofspaces<<endl;

string spaceLine;
vector<string> tempVec;

tempVec.push_back("hello");
tempVec.push_back("world");
tempVec.push_back("apple");
tempVec.push_back("cat");
tempVec.push_back("hello");
tempVec.push_back("world");
tempVec.push_back(" ");
tempVec.push_back(" ");

spaceLine.insert(0, noofspaces, ' ');
cout << "Spaces Line:" <<spaceLine<<"endl"<<endl;

for(int i = 0 ; i < tempVec.size(); i++){
cout << tempVec[i]<<endl;
}
addToMap(tempVec,mymap);
addSpacesToMap(spaceLine,mymap);

return 0;
}

string parsingForPunctuations(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 parsingForSpaces(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>(word[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
Nov 21 '08 #1
Share this Question
Share on Google+
8 Replies


boxfish
Expert 100+
P: 469
space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
Expand|Select|Wrap|Line Numbers
  1. space.substr(i, i + 1)
I don't know if this will make your code work correctly, but it will clear up the error.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
Hope this helps.
Nov 21 '08 #2

P: 19
space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
Expand|Select|Wrap|Line Numbers
  1. space.substr(i, i + 1)
I don't know if this will make your code work correctly, but it will clear up the error.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
Hope this helps.
I'll try that mate thanks!
Nov 21 '08 #3

Expert 100+
P: 2,400
In your example the substring "I'm" is broken into the following words:
> one instance of single-quote (')
> one instance of "Im".

Are you sure that's what you want? I could understand this input being interpreted as one word ("I'm") or three words ("I", "'", "m"), but I'm quite surprised that you want to pluck a punctuation mark from the middle of a word.
Nov 21 '08 #4

P: 19
In your example the substring "I'm" is broken into the following words:
> one instance of single-quote (')
> one instance of "Im".

Are you sure that's what you want? I could understand this input being interpreted as one word ("I'm") or three words ("I", "'", "m"), but I'm quite surprised that you want to pluck a punctuation mark from the middle of a word.
This is not my assignment mate... yeah this is how my instructor wants... basically I making a word frequency map to be encoded using a huffman code.

project is read in a very large text file, and encode it.
Nov 21 '08 #5

P: 19
space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
Expand|Select|Wrap|Line Numbers
  1. space.substr(i, i + 1)
I don't know if this will make your code work correctly, but it will clear up the error.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
Hope this helps.

i did what you said this is the output i get:

vector "hello"
vector "world"
vector "apple"
vector "cat"
vector "hello"
vector "world"
vector " "
vector " "
vector " "
vector " "
vector " "
vector " "
vector " "
map 2
map 2
map 2
map 1
map apple 1
map cat 1
map hello 2
map world 2

i changed it a bit. rather than calling a method addSpaceToMap() i loop through the string of spaces and i add to tempVec (if you remember this is a vector).

Expand|Select|Wrap|Line Numbers
  1. for(int x = 0 ; x < spaceLine.length() ; x++){
  2.         cout <<spaceLine[x]<<endl; 
  3.         tempVec.push_back(spaceLine.substr(x, x+1));
  4.     }

there are spaces of different size.... any suggestion to fix it?!?

you dont see it in the output but quotes are of different sizes...

also thanks for fixing the error!
Nov 21 '08 #6

boxfish
Expert 100+
P: 469
I am using this code in the main function,
Expand|Select|Wrap|Line Numbers
  1.     spaceLine.insert(0, noofspaces, ' ');
  2.     cout << "Spaces Line:" << spaceLine <<"endl"<<endl;
  3.  
  4.     for(int i = 0 ; i < tempVec.size(); i++){
  5.         cout << "\"" << tempVec[i] << "\"" << endl;
  6.     }
  7.     addToMap(tempVec,mymap);
  8.     for(int x = 0 ; x < spaceLine.length() ; x++){ 
  9.         cout << "\"" << spaceLine[x] << "\"" << endl;  
  10.         tempVec.push_back(spaceLine.substr(x, x+1)); 
  11.     }
  12.  
and I am not getting spaces of different widths.
So can you please post some more of the modifications you have made?
Nov 22 '08 #7

P: 19
@boxfish
I figured it out! But couldn't have done it without your help mate!

instead of => tempVec.push_back(spaceLine.substr(x, x+1));
I did => tempVec.push_back(spaceLine.substr(x, 1));

When its x+1 the first substring will be an empty string with size 1 an second will be empty string with size 2 and so on... Because we loop through the string.

tempVec.push_back(spaceLine.substr(x, 1)); does what I want it to do. I didn't think about it much after that, because I ran into another problem. I wanted to post this early but bytes was down for a long time on Saturday.

Again thanks for you help!

drjay
Nov 24 '08 #8

boxfish
Expert 100+
P: 469
Oh, sorry, I see I was wrong about that. Argument 2 is the size of the substring. I'm glad you got it working though, and I'm glad I was of some help.
Nov 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.