• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:52
CET 14:52
KST 22:52
  • 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
ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8
Community News
Weekly Cups (March 9-15): herO, Clem, ByuN win12026 KungFu Cup Announcement6BGE Stara Zagora 2026 cancelled12Blizzard Classic Cup - Tastosis announced as captains18Weekly Cups (March 2-8): ByuN overcomes PvT block5
StarCraft 2
General
Weekly Cups (August 25-31): Clem's Last Straw? Blizzard Classic Cup - Tastosis announced as captains Weekly Cups (March 9-15): herO, Clem, ByuN win Potential Updates Coming to the SC2 CN Server Weekly Cups (March 2-8): ByuN overcomes PvT block
Tourneys
2026 KungFu Cup Announcement [GSL CK] #2: Team Classic vs. Team Solar [GSL CK] #1: Team Maru vs. Team herO RSL Season 4 announced for March-April PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar)
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026] Map Editor closed ?
External Content
The PondCast: SC2 News & Results Mutation # 517 Distant Threat Mutation # 516 Specter of Death Mutation # 515 Together Forever
Brood War
General
ASL21 General Discussion BGH Auto Balance -> http://bghmmr.eu/ Gypsy to Korea BW General Discussion BSL 22 Map Contest — Submissions OPEN to March 10
Tourneys
[Megathread] Daily Proleagues [BSL22] Open Qualifiers & Ladder Tours IPSL Spring 2026 is here! ASL Season 21 Qualifiers March 7-8
Strategy
Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2 Fighting Spirit mining rates Zealot bombing is no longer popular?
Other Games
General Games
General RTS Discussion Thread Nintendo Switch Thread Stormgate/Frost Giant Megathread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion The Story of Wings Gaming
League of Legends
FTM 2019 new update 24.2.2
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Need some new stuff (electronics) ~ Mexico's Drug War Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread Formula 1 Discussion Tokyo Olympics 2021 Thread General nutrition recommendations Cricket [SPORT]
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2189 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
Kung Fu Cup
11:00
2026 Weekly #1
WardiTV813
TKL 224
Rex140
IndyStarCraft 130
EnkiAlexander 23
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko372
TKL 224
Rex 140
IndyStarCraft 130
ProTech30
Trikslyr26
elazer 14
RushiSC 6
StarCraft: Brood War
Britney 45067
firebathero 10311
Calm 10084
Bisu 2847
Horang2 2077
Jaedong 1371
Soma 522
BeSt 480
Mini 460
Light 312
[ Show more ]
EffOrt 301
Pusan 254
actioN 247
Snow 240
Zeus 235
Rush 188
Last 130
Soulkey 96
hero 75
Aegong 69
ToSsGirL 57
Hyun 51
Backho 51
JYJ 43
sorry 35
Killer 34
sSak 31
JulyZerg 30
Barracks 28
Hm[arnc] 27
[sc1f]eonzerg 22
IntoTheRainbow 19
Bale 19
GoRush 18
soO 17
HiyA 14
ajuk12(nOOB) 13
zelot 12
SilentControl 12
Noble 11
Terrorterran 11
Sacsri 9
Dota 2
Gorgc2629
canceldota153
League of Legends
JimRising 180
Counter-Strike
fl0m2656
pashabiceps1335
x6flipin503
oskar50
kRYSTAL_29
Super Smash Bros
Mew2King82
Heroes of the Storm
Khaldor125
Other Games
singsing2242
B2W.Neo680
shoxiejesuss282
DeMusliM211
Happy193
Fuzer 182
mouzStarbuck127
XaKoH 119
Hui .109
Organizations
Dota 2
PGL Dota 2 - Main Stream203
StarCraft: Brood War
lovetv 14
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos2141
• Stunt585
Other Games
• WagamamaTV116
Upcoming Events
OSC
10h 8m
The PondCast
20h 8m
KCM Race Survival
20h 8m
WardiTV Team League
22h 8m
OSC
23h 8m
Replay Cast
1d 10h
KCM Race Survival
1d 20h
WardiTV Team League
1d 22h
Korean StarCraft League
2 days
RSL Revival
2 days
Maru vs Zoun
Cure vs ByuN
[ Show More ]
uThermal 2v2 Circuit
3 days
BSL
3 days
RSL Revival
3 days
herO vs MaxPax
Rogue vs TriGGeR
BSL
4 days
Replay Cast
4 days
Replay Cast
4 days
Afreeca Starleague
4 days
Sharp vs Scan
Rain vs Mong
Wardi Open
4 days
Monday Night Weeklies
5 days
Sparkling Tuna Cup
5 days
Afreeca Starleague
5 days
Soulkey vs Ample
JyJ vs sSak
Afreeca Starleague
6 days
hero vs YSC
Larva vs Shine
Liquipedia Results

Completed

Proleague 2026-03-16
WardiTV Winter 2026
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Jeongseon Sooper Cup
BSL Season 22
CSL Elite League 2026
RSL Revival: Season 4
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

ASL Season 21
Acropolis #4 - TS6
2026 Changsha Offline CUP
Acropolis #4
IPSL Spring 2026
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
NationLESS Cup
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
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 © 2026 TLnet. All Rights Reserved.