Converting strings to other types


 
Thread Tools Search this Thread
Top Forums Programming Converting strings to other types
# 1  
Old 02-05-2014
Converting strings to other types

I am reading command line arguments passed through a c++ program.

I am using the following


Code:
 
string  arg_genr;
int ngenr;
istringstream iss_ngenr(arg_genr);
iss_genr >> ngenr;
 
string  arg_tstep;
float tstep;
istringstream iss_tstep(arg_tstep);
iss_tstep >> tstep;

I wonder what people think of this method of converting strings to other types. Is this a good way to do it?
# 2  
Old 02-05-2014
It works. It's inefficient, and difficult to do anything fancy with it, but it's also difficult to get it wrong unlike some faster methods.
# 3  
Old 02-05-2014
What do you suggest instead?

Are stoi, stof, and stod slow as well?

---------- Post updated at 12:46 PM ---------- Previous update was at 12:10 PM ----------

I am aware Boost lexical cast is slow as well

Last edited by kristinu; 02-05-2014 at 01:20 PM..
# 4  
Old 02-05-2014
stoi, stof, etc are likely as fast as you can get. Error detection will be more difficult however, they do not throw anything.

Also, ask yourself whether performance is important. Are you processing thousands of numbers, or just two?
# 5  
Old 02-05-2014
Seems that using boost spirit might give better performance. Have you ever tried using spirit?
Will be passing a few tens of numbers.
# 6  
Old 02-05-2014
Boost is enormous. It's difficult to believe Boost could improve the performance of anything -- mostly it's a programmer convenience.

For tens of numbers, who cares? You have a method that works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

3. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

6. UNIX for Dummies Questions & Answers

Un-compression types...

Hi Folks, As I am familiar wih both types compresion forms: gun-zip and .rpm. My questions is how do I uncompress gunz.zip type? As the .rpm I can double click and it will extract...Can someone shed some light on this and thank you... M (2 Replies)
Discussion started by: Mombo_Z
2 Replies

7. UNIX for Dummies Questions & Answers

mime types

Hi, I am trying to launch an ogg movie from a pdf file which has been produced with pdflatex and \movie {\centerline{\includegraphics {grafiques_xerrades/un_manolo_amb_camera.pdf}}} {hlims_xerrades/XocCumuls.ogg} The switch "externalviewer" makes kpdf launch the default... (5 Replies)
Discussion started by: pau
5 Replies

8. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies

9. UNIX for Dummies Questions & Answers

So many types of LINUX's!

I installed Redhat into my system. The reason? This was the version my friend was running and he told me about this one, so I downloaded and installed it. Simple enough :D But as I am searching the net, I am coming across many other forms of linux made by other companies. Redhat seems to be... (2 Replies)
Discussion started by: Minnesota Red
2 Replies

10. UNIX for Dummies Questions & Answers

servers types

dear sir , i would like to ask about sun solaries servers generations ? i hear about sparc and ultra . i want to know the versions , and is there other servers types ?? Thank (3 Replies)
Discussion started by: tamemi
3 Replies
Login or Register to Ask a Question