![]() |
|
#1
|
|||
|
|||
C++ style strings and STLNow, I've been told time and time again, not to make my own home-brewed linked-lists and such, but to use STL instead... the problem is that I can't seem to make C++ strings (yes, I know I can use char*, but I really like all the handy built-in functions of strings)... or other custom structures to work via STL...
Is there anyway to make these structs or strings work, without creating a new class to handle each one every time the need arises... maybe by via inheritence or something? |
|
#2
|
|||
|
|||
|
Quote:
Look for an on-line tutorial, such as http://www.yolinux.com/TUTORIALS/Lin...ringClass.html You can set one string equal to another (no more strcpy()); you can compare strings with "==" (no more strcmp). You can use string variables without knowing the size ahead of time and without having to use dynamic storage allocation. Here is a taste of some simple things you can do: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
|
Erm, I do know how to use strings... the problem is using them with STL containers... :p ... I should probably give an example...
CPP / C++ / C Code:
A program like that... would give me: test.cpp(18) : warning C4786: 'std::reverse_bidirectional_iterator<std::list<std ::basic_string<char,std::char_traits<char>,std::al locator<char> >,std::allocator<std::basic_string<char ,std::char_traits<char>,std::allocator<char> > > >::iterator,std::basic_string<char,std::char_trait s<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > &,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information test.cpp(18) : warning C4786: 'std::reverse_bidirectional_iterator<std::list<std ::basic_string<char,std::char_traits<char>,std::al locator<char> >,std::allocator<std::basic_string<char ,std::char_traits<char>,std::allocator<char> > > >::const_iterator,std::basic_string<char,std::char _traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const &,std::basic_string<char,std::char_tra its<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information c:\program files\microsoft visual studio\vc98\include\list(131) : warning C4786: 'std::list<std::basic_string<char,std::char_traits <char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >: :list<std::basic_string<char,std::char_traits<char >,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information c:\program files\microsoft visual studio\vc98\include\list(153) : warning C4786: 'std::list<std::basic_string<char,std::char_traits <char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >: :~list<std::basic_string<char,std::char_traits<cha r>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information And you know, I only just realised they were only warnings... but still, what's the go here...? And when doing the example, for some bizarre reason structs worked fine this time... I think I'm going to have to fiddle to find out what the problem was before... :roll: |
|
#4
|
||||
|
||||
|
Quote:
1) So you learn the techniques 2) So you can make the program/system do things the STL won't allow 3) So you can translate what you've learned to other things similar but not in the STL 4) So you can feel good about yourself Why learn sorting when qsort() is standard? So you can sort faster. Why learn how to convert strings of numbers into integers when there's sscanf() and atoi()? So you can parse without errors. etc... Don't listen to the people that tell you not to learn a technique that's useful. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#5
|
|||
|
|||
|
Quote:
Well, asking the right question would help. You should always: 1. State the problem 2. State what you did (like giving an example) 3. State what you expected to get 4. State what you got It doesn't hurt to state what compiler and operating system you are using. Your example compiled OK for me with Borland, Microsoft, and GNU compilers. Of course it didn't do anything. Try this: CPP / C++ / C Code:
To the best of my knowledge this is absolute standards-compliant C++ code. If you get any warnings or errors, I would really like to know it. Regards, Dave Last edited by davekw7x : 31-Dec-2004 at 10:00.
|
|
#6
|
|||
|
|||
|
Yeah, sorry about that, I see it happen all the time and then I go and do it myself...
Anyway, I thought I'd tested it with gcc in Dev-C++, but it turns out that the warnings only occur in msvc++ 6.0... I might pull grab a copy of the compiler for msvc++ 7.0 and test it there. Thanks for the help... and yeah, Walt, I'll still home-brew custom classes mainly for those reasons... I've mainly been told that it's better to use the others for portability reasons, plus the fact that they're often optimized as much as possible. |
|
#7
|
|||
|
|||
|
Quote:
I compiled your code (as well as mine) with Microsoft Visual C++ 6.0 (also with the C++ in .net). No errors, no warnings. Regards, Dave |
|
#8
|
||||
|
||||
|
Quote:
That's odd... I compiled his code with Visual C++ 6.0 and it gave me the warnings, but not with .NET. Are you sure you didn't disable this kind of warnings? Anyway, these warnings should be ignored IMHO. They are just the proof that VC6 has some serious problems with it's STL implementation. Best regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#9
|
|||
|
|||
|
Quote:
Well, I always compile console applications with the comand line compiler and execute from the command prompt. Quote:
Maybe the warnings come when compiling from the IDE. I can't imagine myself making a project and going through the IDE when learning (or using) console applications. (I should know better than to make assumptions about how other people are operating before I make pronouncements like "no hits, no runs, no errors, no etc.") Your Mileage May Vary. Regards, Dave |
|
#10
|
|||
|
|||
|
Okay, via the command-line, I narrowed it down to the /Gm /ZI switches... I'm not entirely sure why, but these are causing the warnings.
Quote:
I can't honestly see why you would compile from the command line... not when you can click errors within the IDE for error correcting to jump straight to them. Especially with a project the scope of mine... it's more than just a single file console app. Anyway... it looks like VC++ 6.0 (except for the MSDN ) is past its use-by date... I'm living off gcc now...Maybe I'll set the Visual C++ Toolkit 2003 compiler up in place of the one packaged with VC++ 6.0. |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The