GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Dec-2004, 09:02
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough

C++ style strings and STL


Now, 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  
Old 30-Dec-2004, 09:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dexter
Now, 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?

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:
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string myfirstname("Zaphod"); // initialization
  string mylastname; // uninitialized

  string userfirstname, userlastname;

  mylastname = "Beeblebrox"; // simple assignment

  cout << "Enter your first name: " << flush;
  cin >> userfirstname; // try entering a reallllllly long first name
  
  cout << "Enter your last name: "<< flush;
  cin >> userlastname;

  if (myfirstname == userfirstname) { // simple comparison
    cout << "Wow, " << userfirstname << ", we have the same first name." 
         << endl;
  }
  else {
    cout << "Hello, " << userfirstname << ", I see that your first name has " 
         << userfirstname.length() << " letters." << endl;
  }

  if (mylastname == userlastname) {
    if (myfirstname == userfirstname) {
      cout << "You are kidding me, right? Or are you really a doppelganger?"
           << endl;
    }
    else {
      cout << "How about that! We have the same last name.  Maybe we are cousins?" 
           << endl;
    }
  }
  return 0;
}

Regards,

Dave
  #3  
Old 30-Dec-2004, 16:14
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
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:
#include <string>
#include <list>

using std::string;
using std::list;

int main()
{

	list<string> ourList;

	string x = "Blah";

	ourList.push_front(x);

	return(0);
}


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  
Old 31-Dec-2004, 00:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dexter
Now, I've been told time and time again, not to make my own home-brewed linked-lists and such, but to use STL instead...
I disagree with what you've been told. Why make a home-brewed linked-list?
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  
Old 31-Dec-2004, 09:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dexter
Erm, I do know how to use strings... the problem is using them with STL containers... ... I should probably give an example...



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:
// Illustrate use of list STL
//
#include <iostream>
#include <string>
#include <list>

using namespace std;

int main()
{
  list<string> ourList;
  list<string>::iterator ourList_iter;

  ourList.push_front ("Tom");
  ourList.push_front ("Dick");
  ourList.push_front ("Harry");

  // Print the list and remove each name from the end of the list.

  cout << endl << "Names:" << endl;
  while (ourList.size() > 0) {
    cout << "   " << ourList.back() << endl;
    ourList.pop_back();
  } 
  return 0;

}

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  
Old 01-Jan-2005, 01:22
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
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  
Old 01-Jan-2005, 06:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dexter
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.

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  
Old 03-Jan-2005, 07:30
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by davekw7x
I compiled your code (as well as mine) with Microsoft Visual C++ 6.0 (also with the C++ in .net). No errors, no warnings.

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  
Old 03-Jan-2005, 08:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by LuciWiz
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

Well, I always compile console applications with the comand line compiler and execute from the command prompt.

Quote:
F:\Home\dave\cprogs\monday\testprog>cl test.cpp /GX
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:test.exe
test.obj

F:\Home\dave\cprogs\monday\testprog>test

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  
Old 03-Jan-2005, 09:17
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
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:
Originally Posted by davekw7x
I can't imagine myself making a project and going through the IDE when learning (or using) console applications.

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 GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.