473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

stl/map and string question

I have string and a map<string,int>

i need to iterate through the map and find string[i] and replace the int with the string.

eg: if map contains

hello 6
world 4
its 3
me 60

if the string is "its me hello world world hello"

output should be "3 60 6 4 4 6"

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0 ; i < str.length() ; i++){
  2.          it = mymap.find(str[i]); //it is iterator, mymap is map<string,int> 
  3.           myOutfile << (*it).second; //myOutfile is out stream
  4.         }
  5.  
i also tried
Expand|Select|Wrap|Line Numbers
  1.  for(itStr its = str.begin() ; its < str.end(); its++ ){
  2.             itf = myMap.find(*its);
  3.             myOutfile << (*itf).second;
  4.         }
i get the same error

main.cpp:294: error: invalid conversion from `char' to `const char*'
main.cpp:294: 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>]'

can someone please help me fix this!

thanks

drjay
Nov 30 '08 #1
6 1648
JosAH
11,448 Expert 8TB
If Str is a single string I can understand the error message because you're iterating over single characters. Single chars can not be converted to pointers to chars.

kind regards,

Jos
Nov 30 '08 #2
what is the way around it?
Nov 30 '08 #3
Ganon11
3,652 Expert 2GB
What you are assuming is that your string is treated as an array of words, and that it has an iterator which accesses each word. This isn't what C++ assumes or performs, though. You can work around it by manually parsing your string to get an array/list of words (hint: each word is separated by a space). Put each word into a list or vector, and use that list/vector's iterator, and the code you have will work fine.
Nov 30 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Your loop in incorrectly coded.

You are looking for strings os the loop does not test i < str.length(). Instead test for the number of strings, that is, the number of elements in the string array.

Expand|Select|Wrap|Line Numbers
  1. for(unsigned int i = 0 ; i <4 ; i++){ 
  2. etc...
  3.  
I made this chamge and your code works fine.
Nov 30 '08 #5
what if the you dont know the length of the string?
Nov 30 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
It's a string object. It knows how long it is.

You can't use a C++ string object using the C-string techniques of C.
Dec 1 '08 #7

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

Similar topics

2
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
5
by: Dave | last post by:
I'm receiving info from a com port into a string. I gradually process the string which constantly shortens it. The question is how long can a string be before I need to write some info to disk...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
39
by: sucaba.r | last post by:
I don't know if this is a unique problem, or I'm going about it the wrong way. I currently connect to one of our SQL servers via a priviliged account (by using RUNAS). Works with no problem. I...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.