a string is pretty much a character array.
[C++] Using std::string as a Function Parameter? - Page 2
Blogs > Revabug |
b3h47pte
United States1317 Posts
a string is pretty much a character array. | ||
Cambium
United States16368 Posts
| ||
![]()
TanGeng
Sanya12364 Posts
int Characters(const std::string &strString); is much preferred. Here you aren't going to run a copy constructor on the string on every call. The const also tells the compiler that you aren't going to change any of the contents of the obj during the call. nvm the edit. | ||
Queequeg
Germany263 Posts
http://www.cplusplus.com/reference/string/string/ std::string is a class, char[] isn't. | ||
coltrane
Chile988 Posts
You can do either: # # using namespace std; after every include to set std for every variable Or std::string any time you define a string variable. This is because the namespace can be different, you could use other Library sets or the same with other names on the Classes. In the default set of librarys you should put like header of your main #include<string.h> using namespace std; And then your function will compile with no problem. | ||
coltrane
Chile988 Posts
On July 21 2009 08:49 2Pacalypse- wrote: I'm a C programmer, and I'm just wondering did C++ abandon pointers completely or can you still use them? Nono, advanced c++ is all about pointers... is much better for making some vector type or list type classes. | ||
Exteray
United States1094 Posts
On July 21 2009 07:51 GogoKodo wrote: Ok after a quick google I found some code using this syntax void function (std::string &str) { } This one includes a "&" which passes the argument by reference. It changes the value of the parameter. Although the program won't change the string, it is unnecessary here. | ||
![]()
TanGeng
Sanya12364 Posts
If the function is used with any frequency, you'll copy strings and destroy strings over and over again. It'll be slow. Classes are not to be passed by value unless absolutely necessary. Start practicing good programming style on the simple examples and it'll come naturally later. | ||
NicolBolas
United States1388 Posts
On July 21 2009 11:20 coltrane wrote: Nono, advanced c++ is all about pointers... is much better for making some vector type or list type classes. I program "advanced" C++ and I don't use pointers. Well, not bare pointers. Manly C++ is done using Boost, and therefore boost::shared_ptr. I have an infrequently updated blog about this stuff. On July 21 2009 09:08 2Pacalypse- wrote: And sorry for taking over the thread, but what's with this string data type? What's the difference between string type and char[]? C++ stuff. First, the full name is "std::string". The "std" part is a namespace. Names in C++ can be put into separate namespaces, so that you don't have type name collisions. Like if you have one library that defines a type called "string", it can put it in the namespace "library". Thus std::string and library::string can live and function within the same program. Second, std::string is a C++ Class. It's sort of like a struct in C, but a lot more intelligent. On the creation of a Class, you are guaranteed that a certain function will be called. This function, the constructor, allows you to do initialization work, which ensures that you never use uninitialized objects. Similarly, when the object is destroyed, another function is called that allows you to do cleanup work. Also, Classes can have functions in them that operate on the given instance of that class. std::string is an intelligent wrapper around a char* string. It's constructor allocates memory for it. If you start adding characters beyond the length of the allocated segment, it will expand the allocated region to enclose them all. It has its own searching and comparison functions. And because its destructor will handle the deletion of the internal char*, it can never leak memory (unless you fail to destroy the std::string, in which case you leaked memory). It is in every way superior to a naked char*. | ||
ven
Germany332 Posts
-_-;; | ||
haduken
Australia8267 Posts
.... Why can't you just use STL libaries for them? | ||
NicolBolas
United States1388 Posts
On July 21 2009 13:08 ven wrote: manly c++ is done without ++ -_-;; I've written straight-C before; that's just self-inflicted torture. Real men don't do things the hard way; they do them the smart way. | ||
![]()
MasterOfChaos
Germany2896 Posts
| ||
KillForce
Sweden36 Posts
this is a good string, which is usually some text followed by a zero. When using this, you will always have to keep the zeros in mind, and allocating your own memory, and just being very focused in general. You'll find yourself doing a lot of malloc/strncpy/strcat, pointer arithmetic and suchlike. It's a hassle, for newcomers and veterans alike. These manual labour strings even harder to do right in C++ than in C, what with exceptions and all. An std::string takes care of the memory, allocating and releasing as needed. In addition, it is also usable as an STL container. I realize as I refresh the thread that the memory argument has been made a few times already. So I'll move on (just adding that I also support passing the string as a constant refererence). As for the OP, the string class you want is std::string, and to use it you must include the string header (#include <string>, NOT <string.h>, string.h might work in some cases, but it is not standard C++). It is true that "using namespace std" solves your immediate problem, but you might end up with bad habits. This is explained here: http://www.parashift.com/c -faq-lite/coding-standards.html#faq-27.5 The C++ FAQ Lite is something I would heavily recommend any c++ programmer to keep in their bookmarks. It is actually quite pragmatic and attempts to explain things instead of just blurting out truths. As for testing for whitespace, writing your own test can be good if you know exactly what you want. But I'm going to throw it out here that there is a function called std::isspace accessible through the ctype header (#include <ctype>) that tests for all the usual whitespace characters (' ', '\t', '\f', '\v', '\r', '\n'). As for whether C++ uses pointers or not, the answer is: yes, it does. Not using them would not be efficient. Some people who has used them enough learn that they are a lot of manual labour, however, and starts to find ways around the hardships. Pointers are dumb and they can become invalid without feedback (if someone else deletes it), or you can forget to delete it yourself (memory leaks and strange behavior). Smart pointers is a collective name for objects that try to manage pointers for you. boost::shared_ptr (http://www.boost.org/) (as pointed to earlier) is a good example of such a pointer, and is due to be included in the next c++ standard. But this is an extensive subject. Sorry for re-iterating a lot of others' posts ![]() | ||
| ||