• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 01:03
CEST 07:03
KST 14:03
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
uThermal's 2v2 Tour: $15,000 Main Event10Serral wins EWC 202544Tournament Spotlight: FEL Cracow 202510Power Rank - Esports World Cup 202580RSL Season 1 - Final Week9
Community News
Weekly Cups (Aug 4-10): MaxPax wins a triple2SC2's Safe House 2 - October 18 & 195Weekly Cups (Jul 28-Aug 3): herO doubles up6LiuLi Cup - August 2025 Tournaments5[BSL 2025] H2 - Team Wars, Weeklies & SB Ladder10
StarCraft 2
General
Weekly Cups (Aug 4-10): MaxPax wins a triple Geoff 'iNcontroL' Robinson has passed away Serral wins EWC 2025 uThermal's 2v2 Tour: $15,000 Main Event The GOAT ranking of GOAT rankings
Tourneys
Global Tourney for College Students in September RSL: Revival, a new crowdfunded tournament series SC2's Safe House 2 - October 18 & 19 Sparkling Tuna Cup - Weekly Open Tournament LiuLi Cup - August 2025 Tournaments
Strategy
Custom Maps
External Content
Mutation # 486 Watch the Skies Mutation # 485 Death from Below Mutation # 484 Magnetic Pull Mutation #239 Bad Weather
Brood War
General
ASL Season 20 Ro24 Groups ASL20 Pre-season Tier List ranking! StarCon Philadelphia BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion
Tourneys
[Megathread] Daily Proleagues KCM 2025 Season 3 Small VOD Thread 2.0 [ASL20] Online Qualifiers Day 2
Strategy
Fighting Spirit mining rates [G] Mineral Boosting Simple Questions, Simple Answers Muta micro map competition
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Total Annihilation Server - TAForever Beyond All Reason [MMORPG] Tree of Savior (Successor of Ragnarok)
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread [\m/] Heavy Metal Thread [Manga] One Piece Movie Discussion! Korean Music Discussion
Sports
TeamLiquid Health and Fitness Initiative For 2023 2024 - 2025 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Gtx660 graphics card replacement Installation of Windows 10 suck at "just a moment" Computer Build, Upgrade & Buying Resource Thread
TL Community
TeamLiquid Team Shirt On Sale The Automated Ban List
Blogs
Gaming After Dark: Poor Slee…
TrAiDoS
[Girl blog} My fema…
artosisisthebest
Sharpening the Filtration…
frozenclaw
ASL S20 English Commentary…
namkraft
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
Customize Sidebar...

Website Feedback

Closed Threads



Active: 497 users

[C++] Using std::string as a Function Parameter? - Page 2

Blogs > Revabug
Post a Reply
Prev 1 2 All
b3h47pte
Profile Blog Joined May 2007
United States1317 Posts
July 21 2009 00:17 GMT
#21
char[] is the C way and string is the C++ way
a string is pretty much a character array.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 21 2009 00:34 GMT
#22
use (const char* string) in your parameter?
When you want something, all the universe conspires in helping you to achieve it.
TanGeng
Profile Blog Joined January 2009
Sanya12364 Posts
Last Edited: 2009-07-21 00:48:56
July 21 2009 00:43 GMT
#23
You should definitely pass by reference.

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.
Moderator我们是个踏实的赞助商模式俱乐部
Queequeg
Profile Joined September 2003
Germany263 Posts
July 21 2009 01:48 GMT
#24
http://lmgtfy.com/?q=std string
http://www.cplusplus.com/reference/string/string/
std::string is a class, char[] isn't.
coltrane
Profile Blog Joined June 2008
Chile988 Posts
July 21 2009 02:17 GMT
#25
look, the thing is simple, the namespaces are wrong.


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.
Jävla skit
coltrane
Profile Blog Joined June 2008
Chile988 Posts
July 21 2009 02:20 GMT
#26
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.
Jävla skit
Exteray
Profile Blog Joined June 2007
United States1094 Posts
July 21 2009 03:02 GMT
#27
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
Profile Blog Joined January 2009
Sanya12364 Posts
July 21 2009 03:17 GMT
#28
Unnecessary, but still good style. Passing by value implicitly triggers the copy constructor on call and a destructor on exit. Passing by reference does neither.

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.
Moderator我们是个踏实的赞助商模式俱乐部
NicolBolas
Profile Blog Joined March 2009
United States1388 Posts
Last Edited: 2009-07-21 04:10:39
July 21 2009 04:00 GMT
#29
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*.
So you know, cats are interesting. They are kind of like girls. If they come up and talk to you, it's great. But if you try to talk to them, it doesn't always go so well. - Shigeru Miyamoto
ven
Profile Blog Joined December 2008
Germany332 Posts
July 21 2009 04:08 GMT
#30
manly c++ is done without ++

-_-;;
You can reach the rainbow. I'll be there to help.
haduken
Profile Blog Joined April 2003
Australia8267 Posts
Last Edited: 2009-07-21 04:12:03
July 21 2009 04:11 GMT
#31
On July 21 2009 11:20 coltrane wrote:
Show nested quote +
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?




....

Why can't you just use STL libaries for them?
Rillanon.au
NicolBolas
Profile Blog Joined March 2009
United States1388 Posts
July 21 2009 04:12 GMT
#32
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.
So you know, cats are interesting. They are kind of like girls. If they come up and talk to you, it's great. But if you try to talk to them, it doesn't always go so well. - Shigeru Miyamoto
MasterOfChaos
Profile Blog Joined April 2007
Germany2896 Posts
July 21 2009 07:31 GMT
#33
use "const string&" for string parameters you don't change
LiquipediaOne eye to kill. Two eyes to live.
KillForce
Profile Joined March 2009
Sweden36 Posts
July 21 2009 11:41 GMT
#34
A char[] is a piece of memory, and it's up to you to make sure that
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 .
Prev 1 2 All
Please log in or register to reply.
Live Events Refresh
Next event in 5h 57m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Livibee 101
StarCraft: Brood War
ggaemo 1195
PianO 150
Snow 98
Tasteless 59
Noble 17
Bale 15
ajuk12(nOOB) 12
Icarus 6
Dota 2
monkeys_forever998
Counter-Strike
Stewie2K546
Super Smash Bros
Mew2King238
Heroes of the Storm
Khaldor145
Other Games
summit1g12635
JimRising 708
WinterStarcraft372
ViBE198
Maynarde119
NeuroSwarm102
trigger4
Organizations
Other Games
gamesdonequick854
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• Sammyuel 314
• practicex 46
• davetesta30
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo1382
Other Games
• imaqtpie551
Upcoming Events
Wardi Open
5h 57m
Wardi Open
9h 57m
RotterdaM Event
10h 57m
Replay Cast
18h 57m
WardiTV Summer Champion…
1d 5h
RSL Revival
1d 11h
PiGosaur Monday
1d 18h
WardiTV Summer Champion…
2 days
The PondCast
3 days
WardiTV Summer Champion…
3 days
[ Show More ]
Replay Cast
3 days
LiuLi Cup
4 days
Online Event
5 days
SC Evo League
5 days
uThermal 2v2 Circuit
5 days
Sparkling Tuna Cup
6 days
WardiTV Summer Champion…
6 days
SC Evo League
6 days
uThermal 2v2 Circuit
6 days
Liquipedia Results

Completed

StarCon 2025 Philadelphia
FEL Cracow 2025
CC Div. A S7

Ongoing

Copa Latinoamericana 4
Jiahua Invitational
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
uThermal 2v2 Main Event
HCC Europe
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

ASL Season 20
CSLAN 3
BSL Season 21
BSL 21 Team A
RSL Revival: Season 2
Maestros of the Game
SEL Season 2 Championship
WardiTV Summer 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
Roobet Cup 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.