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
8 4667
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:
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.
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:
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!
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.
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.
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:
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). - for(int x = 0 ; x < spaceLine.length() ; x++){
-
cout <<spaceLine[x]<<endl;
-
tempVec.push_back(spaceLine.substr(x, x+1));
-
}
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!
I am using this code in the main function, -
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);
-
for(int x = 0 ; x < spaceLine.length() ; x++){
-
cout << "\"" << spaceLine[x] << "\"" << endl;
-
tempVec.push_back(spaceLine.substr(x, x+1));
-
}
-
and I am not getting spaces of different widths.
So can you please post some more of the modifications you have made?
@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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mark |
last post by:
My PHP script builds a table that is too wide to fit on the paper. Two
of the columns contain strings that are more lengthy than data in the
other columns.
I can get the table to fit by letting...
|
by: Papajo |
last post by:
Is there a script that would add a space in front of and behind any
text adding to a form box, it coud be triggered by either an onClick or
onBlur event handler. Any help s appreciated. Thanks Joe
|
by: VM |
last post by:
I'd like to add several spaces to a string variable. For example, if the
value is "VM", I'd like to add 7 spaces so the new value is "VM ".
Is that possible?
Thanks.
|
by: Xah Lee |
last post by:
Tabs versus Spaces in Source Code
Xah Lee, 2006-05-13
In coding a computer program, there's often the choices of tabs or
spaces for code indentation. There is a large amount of confusion about...
|
by: Duncan Dimech |
last post by:
Dear All
I am writing a tool which requires to have controls added to it dynamically.
To make the task more complex, the addition of the control cannot happen
anywhere but it has to be instead of...
|
by: MaRkHaSBEEnMade |
last post by:
Hi there
I currently have a listbox that displays provinces,regions and suburbs.I want to pad the items logically with spaces,but the spaces are being igonred...
eg
Province1
...
|
by: DanielJohnson |
last post by:
I am writing a program in which I am removing all the spaces from the
string. I thought that I could do it two ways. One was parsing the
string character by character and copying onto another...
|
by: psbasha |
last post by:
Hi
I would like to edit the string in a give line of string.
Sample
>>> str1 = 'Pnt1 1000 1 2 3 '
>>> str2 = str1
>>> str3 = '2000 '
>>> str4 = str2 + str3 +...
|
by: Ajm113 |
last post by:
For some reason when I try to pass a string that is this:
'man!';
Into the first param of copy and the value of 4 in to the second param and 6 into the last argument for some reason copy is...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |