c++ code snippets site

A

asmirnov1234567890

Hi

I'd like to place this code to C++ code snippets site.
Does anyone know about where is could be?

Andrei Smirnov

=======================================
//
// example of how to strip std C++ string of empty symbols
// in header and trailer
//

#include <iostream>
#include <string>
using namespace std;

void string_strip(string* s)
{
static const char* empties = " \t\n\r";
int b = s->find_first_not_of(empties);
if (b == -1) {
*s = string();
return;
}
int e = s->find_last_not_of(empties);
*s = string(*s, b, e + 1 - b);
}

int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss;
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}
 
M

Mike Wahler

Hi

I'd like to place this code to C++ code snippets site.

Why? What does it do that you think others would
find useful? It's just plain wrong.
Does anyone know about where is could be?

I'm not sure which site you mean. I do know of one called
www.snippets.org

You can visit there to find out if/how code submissions
are done.
=======================================
//
// example of how to strip std C++ string of empty symbols
// in header and trailer
//

#include <iostream>
#include <string>
using namespace std;

void string_strip(string* s)

Why a pointer? Why not a reference?
{
static const char* empties = " \t\n\r";
int b = s->find_first_not_of(empties);

The return type of 'std::string::find_first_not_of()' is
'std::string::size_type', not 'int'. This is the case
for all the string's 'find' functions.
if (b == -1) {

Why are you checking for -1. If 'find_first_not_of()' does
not find anything, it returns the value 'std::string::npos',
whose value is implementation defined. It could be -1, it
could be some other value.
*s = string();

return;
}
int e = s->find_last_not_of(empties);

Again, wrong type being used to store return value.
*s = string(*s, b, e + 1 - b);
}

int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};

Why use an array when you have the much safer and easier
to use containers, e.g. vector?
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss;
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}


I very much doubt the above code would be accepted by a
site offering code snippets. It's got many problems.

If you scan the archives of this group, you'll find several
much superior solutions than yours.

-Mike
 
A

asmirnov1234567890

Mike said:
Why? What does it do that you think others would
find useful? It's just plain wrong.

it has some features to tease geeks like you :)
I'm not sure which site you mean. I do know of one called
www.snippets.org

this is useless crap indeed. my favorite was 'portable string class' :)
Why a pointer? Why not a reference?

not of you business. i like pointers, there were here before stroustrup
decided to win battle with fortran and introduced refs solely to
support
operations overloading (unfortunately this created good breed
environment
for people who tread ANSI C++ standard as bible).
The return type of 'std::string::find_first_not_of()' is
'std::string::size_type', not 'int'. This is the case
for all the string's 'find' functions.

could you give me the list of compilers/platforms where is matters?
Why are you checking for -1. If 'find_first_not_of()' does
not find anything, it returns the value 'std::string::npos',
whose value is implementation defined. It could be -1, it
could be some other value.

could you imagine any other value than -1. think hard...
.....
int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};

Why use an array when you have the much safer and easier
to use containers, e.g. vector?

because i like everything unsafe and hard. this is my choice.
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss;
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}


I very much doubt the above code would be accepted by a
site offering code snippets. It's got many problems.


it was accepted by usenet already without asking you.
If you scan the archives of this group, you'll find several
much superior solutions than yours.

could you be more specific? i just want the function which strips
spaces
out of my string. i don't need superior solution of this apparently
'very
big' problem.

Andrei

PS: if you are so cool with standard can you tell me what virse 2 of
12.1.5
said. you've got thirty seconds, otherwise you are not cool. time is
running...
 
V

Victor Bazarov

[... insulting and rude response snipped ...]

Perhaps you could be a bit less "cool" and a bit more tactful.
Just a thought. More like dream, probably.

Spend more time lurking around instead of suggesting "solutions"
and insulting regulars, and perhaps everybody is going to have
even more pleasant time visiting comp.lang.c++.
 
M

Mike Wahler

it has some features to tease geeks like you :)

Which are?
this is useless crap indeed.


Usefulness is in the eye of the beholder. I for one, have
found some of the code there useful for learning purposes.
If others have found it useless, that doesn't change what
it's done for me.
my favorite was 'portable string class' :)


not of you business.
i like pointers,

Why? IMO they are one of the major sources of bugs.
there were here before stroustrup
decided

So you seem to be basing your claim of their superiority
over references upon their tenure. I find that to be
very poor logic. Abacuses have been around for thousands
of years. Are they superior to the modern digital computer?
to win battle with fortran

Perhaps you should read some of Stroustrup's writings (e.g.
his "Design and Evolution of C++") before you try to put words
into his mouth.
and introduced refs solely to
support
operations overloading

Repeat my last paragraph above.
(unfortunately this created good breed
environment
for people who tread ANSI C++ standard as bible).

I treat the C++ standard for what it is: the single authoritative
definition of the C++ language. I exploit the guarantees it
grants to me regarding my C++ programs, when translated by
implementations which adhere to it.

could you give me the list of compilers/platforms where is matters?

No need. I have the language standard to guarantee behavior
(if I follow the rules.) One such rule is that the 'std::string'
'find' functions report 'not found' with a value denoted by
the expression 'std::string::npos'. No other value is given
this guarantee, nor is there any guranteee of a particular
actual value for 'npos'.
could you imagine any other value than -1. think hard...

Any negative value, or any positive value outside the
range of 'std::string::size_type'.

....
int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};

Why use an array when you have the much safer and easier
to use containers, e.g. vector?

because i like everything unsafe and hard. this is my choice.

So be it. I'll add your name to my 'no-hire' list. :)
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss;
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}


I very much doubt the above code would be accepted by a
site offering code snippets. It's got many problems.


it was accepted by usenet already without asking you.


Usenet is not a person which can make decisions or 'accept'
or 'reject' something. It's just a 'place'. A tree will
'accept' me shooting it with a shotgun, that doesn't stop
such an action from being detrimental to the tree.
could you be more specific?

Um, no I won't do your research for you. I've already pointed
out a promising place for you to look.
i just want the function which strips
spaces
out of my string.

And there are many which have already been posted to
Usenet (and the web) many times.
i don't need superior solution

Yet you claim to have created one, but have failed
to demonstrate its superiority.
of this apparently
'very
big' problem.

I don't find it a big problem at all. I've long ago solved
it, in many different languages.
Andrei

PS: if you are so cool with standard

I'm not, and never have claimed to be 'cool with standard'.
However I do possess a copy of the ISO standard for C++, and
refer to it when I need to know a conclusive answer to a
question about C++.
can you tell me what virse 2 of
12.1.5
said.

That one's easy. It doesn't say anything, because it does not exist.
you've got thirty seconds,

I'll take as much time as I need, thank you, until such time
as you're paying me to do things for you.
otherwise you are not cool.

My goal is not and never has been to be 'cool'.
time is
running...

Always has been, always will. So what?


OK, so you're 'cool'. I'm productive. That's the way I like it.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Lexical Analysis on C++ 1
Two C++ snippets for brainstorming 6
Crossword 2
Fibonacci C code 1
C language. work with text 3
TF-IDF 1
My Status, Ciphertext 2
Scanf is being prioritized over printf ? 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top