vectors

Pages: 12
[EDIT 18/4/2013]: Happily, the restriction mentioned below has been lifted in C++11, so locally defined classes are useful after all! Thanks to commenter bamboon.

The ability to define classes locally would make creating custom functors (classes with an operator()(), e.g. comparison functions for passing to std::sort() or "loop bodies" to be used with std::for_each()) much more convenient.

Unfortunately, C++ forbids using locally-defined classes with templates, as they have no linkage. Since most applications of functors involve template types that are templated on the functor type, locally defined classes can't be used for this -- you must define them outside the function. :(

[EDIT 1/11/2009]

The relevant quote from the standard is:

14.3.1/2: .A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

http://stackoverflow.com/questions/876048/why-can-i-define-structures-and-classes-within-a-function-in-c
Hardly. It's far more dangerous, harder to manage, and way too early of an optimization. Use a std::vector<std::string>. memory usage should be the least of your concerns unless you profile you code and find that this is the only way to reduce memory usage, even even so this will hardly help.

far more dangerous? how bad with pointers are you? it was very easy for me to manage. is there really such a thing as too early optimization? i need it as compact as possible so std::vector<std::string> would be a) more resource heavy and b) pointless when char *extensions[] would be just as easy to use here.

edit:
also,
xkcd reference wrote

XD thank you for that
Last edited on
xkcd reference wrote:
far more dangerous? how bad with pointers are you?
I am excellent with pointers, which also means I know when and when not to use them.
xkcd reference wrote:
is there really such a thing as too early optimization?
Yes, it's very prevalent with people new to programming - all they think about is wanting their code to be fast and memory efficient, but they don't realize that compilers and computers already make the recommended code fast and memory efficient.
xkcd reference wrote:
i need it as compact as possible so std::vector<std::string> would be a) more resource heavy
Are you holding a vector of billions of strings? Even then, maybe there's at most a couple seconds difference between character pointers and C arrays.
xkcd reference wrote:
and b) pointless when char *extensions[] would be just as easy to use here.
The C++ community at large has agreed time and time again that vectors and strings are easier to use than C-style strings and C arrays in almost all cases.
:L this arguing is pointless. it gets us nowhere and bugs other people on this thread. I know what im doing which is the most important thing. /me waves goodbye
This argument is not an argument, it's a series of questions and answers. Feel free to ignore my advice if you know for a 100% fact that you're right and I'm wrong ;p
Last edited on
closed account (iAk3T05o)
I just want to know what "benefit" C strings have over C++ strings and what possible reason you have to optimize a < 100 line program. If it was an something else with pointers, i'ld get it, but C strings over C++? Seriously?
You want to learn pointers? Fine. You want to learn pointers and you start with C strings (which tutorials ask to skip)? Bad.
Enlighten me.
he never said hes optimizing. i said i was optimizing my compiler. he said he wants to learn pointers, and just because std::string is great, doesnt mean using char *'s is bad. i try to use char *'s daily because you never know when you might have to write c code
closed account (iAk3T05o)
Your post is funny. "when you have to write c code", how many people do you think do that.
You know some people with way more experience haven't written C code in years.
python is written in c. php is written in c. the linux os is written in c. i dont care if some people with way more experience havent written c code in years. that doesnt mean anything. i work with a very respected developer who usually picks c over c++ code.
Nathan2222 wrote:
Your post is funny. "when you have to write c code", how many people do you think do that.
You know some people with way more experience haven't written C code in years.
I'd be surprised if there were less active C projects being developed than C++ projects. Saying that nobody writes in C is just plain wrong.
Here are some examples where I'd use C-strings over C++ strings.
1) 90% of the driver APIs I interface with are in C. Sometimes it's easier to work with C-strings here, especially if you have hard-coded literals.
2) When developing real-time applications and performance is a concern (as in my current project), I avoid dynamic memory. Using vector<string> really has a significant performance hit when compared to char**, especially if you constantly add/remove/construct/destruct the object during the real-time cycle.

Going OO is great when you have very complex projects, but if you can have limited-scope, and well-defined modules, there is no reason why a lower-level paradigm won't work. Actually, low-level usually has much better performance and so implementing OO paradigms when it's not required can introduce overhead that slows you down.

95% of the time I use std::string, but I'm not exclusive to it.
Topic archived. No new replies allowed.
Pages: 12