Click here to Skip to main content
15,887,135 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Read String Pin
Aescleal2-Aug-10 3:51
Aescleal2-Aug-10 3:51 
AnswerRe: Read String Pin
Luc Pattyn2-Aug-10 3:57
sitebuilderLuc Pattyn2-Aug-10 3:57 
AnswerRe: Read String Pin
Chris Losinger2-Aug-10 4:14
professionalChris Losinger2-Aug-10 4:14 
GeneralRe: Read String Pin
Cedric Moonen2-Aug-10 4:33
Cedric Moonen2-Aug-10 4:33 
GeneralRe: Read String Pin
Iain Clarke, Warrior Programmer3-Aug-10 2:09
Iain Clarke, Warrior Programmer3-Aug-10 2:09 
AnswerRe: Read String Pin
Nemanja Trifunovic2-Aug-10 4:58
Nemanja Trifunovic2-Aug-10 4:58 
AnswerRe: Read String Pin
Moak2-Aug-10 5:20
Moak2-Aug-10 5:20 
GeneralRe: Read String Pin
Emilio Garavaglia2-Aug-10 20:25
Emilio Garavaglia2-Aug-10 20:25 
I think is interesting to compare the solutions.
Note: The "trivial" algorithm is to scan the string and count the ','.

Now:

Superman: Just did that using CRT: no additional data are added, and the strchr function is made walking from ',' to ','.

Aescleal: Made an elegant use of STL algorithms.
But: a constant literal is copied into a temporary std::string (probably with also a dynamic array allocated by the class itself), creates two temporary iterators, passing them to the std::count algorithm that just do the loop.

Luc Pattyn: Use a more concise loop.

Chris Losinger: same. Very elegant, but more cryptic.

Nemanja Trifunovich: Same a Aescleal, but also propose to use std::cout with const char* instead of std::string::iterator-s, eliminating the need of the conversions.

Moral of the story: I'm starting to believe that STL - and in particular "strings" are over-evaluated.
The more coincise and elegant (together) is probably the second Nemanja proposal.
But one question makes me wonder:
Suppose I'm a coder asked to do this task, and suppose I know the algoritm, but i'm not expert in the language libraries: what should be fastest for me?

- Read the STL documentation -probaly in strict alphabetic order- trying to guess is there can be something that can help me in counting the ',' (I'm luky the <algorithm> header come as first and the count function is one of the firsts ... estimated time: 15 minutes - but supposing I already know the "iterator"/"container"/"algorithm" model)

- Just write
unsigned count_chars(const char* s, char d)
{
    unsigned c=0;
    for(unsigned int i=0; s[i]; ++i)
        if(s[i]==d) ++c;
    return c;
}

actual time 2 minutes! And the best of efficiency (no spurious allocation and copies)

<sarcasm>Do yo understand the beauty of STL ?!?</sarcasm>

(NOTE: using indexes, instead of incrementing pointers, is actually more efficient when translated into todays processor's code)

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:


GeneralRe: Read String Pin
Moak2-Aug-10 22:26
Moak2-Aug-10 22:26 
GeneralRe: Read String Pin
Emilio Garavaglia3-Aug-10 20:17
Emilio Garavaglia3-Aug-10 20:17 
GeneralRe: Read String Pin
Moak3-Aug-10 22:22
Moak3-Aug-10 22:22 
QuestionCOM interface disappears from ROT [modified] Pin
Phil J Pearson2-Aug-10 1:09
Phil J Pearson2-Aug-10 1:09 
AnswerRe: COM interface disappears from ROT Pin
Code-o-mat2-Aug-10 1:18
Code-o-mat2-Aug-10 1:18 
GeneralRe: COM interface disappears from ROT Pin
Phil J Pearson2-Aug-10 1:51
Phil J Pearson2-Aug-10 1:51 
GeneralRe: COM interface disappears from ROT Pin
Code-o-mat2-Aug-10 1:59
Code-o-mat2-Aug-10 1:59 
GeneralRe: COM interface disappears from ROT Pin
Phil J Pearson2-Aug-10 2:07
Phil J Pearson2-Aug-10 2:07 
AnswerRe: COM interface disappears from ROT Pin
Phil J Pearson2-Aug-10 2:19
Phil J Pearson2-Aug-10 2:19 
GeneralRe: COM interface disappears from ROT Pin
Code-o-mat2-Aug-10 2:24
Code-o-mat2-Aug-10 2:24 
QuestionClistCtrl Pin
alexander 19831-Aug-10 23:59
alexander 19831-Aug-10 23:59 
AnswerRe: ClistCtrl Pin
«_Superman_»2-Aug-10 0:14
professional«_Superman_»2-Aug-10 0:14 
AnswerRe: ClistCtrl Pin
David Crow2-Aug-10 2:49
David Crow2-Aug-10 2:49 
AnswerRe: ClistCtrl Pin
bleedingfingers2-Aug-10 3:18
bleedingfingers2-Aug-10 3:18 
AnswerRe: ClistCtrl Pin
KarstenK2-Aug-10 4:02
mveKarstenK2-Aug-10 4:02 
QuestionGet session based Cookies in C++ BHO Pin
Aendy1-Aug-10 22:16
Aendy1-Aug-10 22:16 
AnswerRe: Get session based Cookies in C++ BHO Pin
«_Superman_»1-Aug-10 23:27
professional«_Superman_»1-Aug-10 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.