You are on page 1of 24

(2013-14)

gepap@aueb.gr

:
string
string


C

: 2013

- 2

null-terminated strings ( )
strings
C++ , string


, Unicode, wide characters .
:
:
C ( 2

( )
3

#include <string>
#include <iostream>
using namespace std;

// string
// string (std::string)

int main() {
string s1, s2; // .
cin >> s1;
// s1 .
s2 = "copy of " + s1; // .
// string.
s1 = "new string";
// s1.
cout << s1 << endl << s2 << endl;
}
"test". :
new string
copy of test
4

string
standard template library (STL)
(.. iostream, string )
,

(namespace), std
string
namespace : std::string
namespace,
:
using namespace std;
string var = my string;
5

string:
char buffer[20] = try me;
std::string str = buffer;

string :
str.c_str();

buffer ():
str.data();

//
// (const)
6

:
string.size()

string.length() ()

:
string.empty()


string:
http://www.cplusplus.com/reference/string/string/

:
string str1 = try me ;
string str2 = now;
string str3 = str1 + str2;

// str3: try me now

str1.append(str2);

// str1: try me now

srt1 += again;

// (append) try me now again

:
cout << str3[2] << endl;
str3[4] = 'i';

// 3 .: y

// 5 .

str3[5] = 't';

// 6 .

cout << str3 << endl;

// try it now
8

:
string str1 = Android;
string str2 = Androids;
bool _2gt1 = str1 < str2;

// _2gt1 == true

str1 = Android4.2;
str2 = Android3.2;
_2gt1 = str1 < str2;
str1 = Andro;
str2 = Andras;
_2gt1 = str1 < str2;

// _2gt1 == false
.


// _2gt1 == false

sub-string:
2
16
string str1 = "I am a healthy camel";
string str2 = "am";
size_t pos = str1.find(str2);

// pos == 2

pos = str1.find(str2);

// pos == 16

10

string to_string (int val);


string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

11

int stoi (const string& str, size_t* idx = 0, int base = 10);
long stol (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const string& str, size_t* idx = 0, int
base = 10);
long long stoll (const string& str, size_t* idx = 0, int base =
10);
unsigned long long stoull (const string& str, size_t* idx = 0,
int base = 10);
float stof (const string& str, size_t* idx = 0);
double stod (const string& str, size_t* idx = 0);
long double stold (const string& str, size_t* idx = 0);

12


,
, :
(7-bit) ASCII,
ISO8859-7
Unicode (8- 16-bit): UTF-8, UTF-16
Linux, UTF-8
wide characters (wchar_t). Windows 16bit wide characters (UTF-16) Linux 32-bit

13


,
( default
C++ C)
1:
locale ( System Locale
!):
string str2 = "";
setlocale(LC_ALL,"Greek");
cout << str2;

14

2:
(UTF16).
(wchar_t char),
w (wide) :
wstring string
wcout cout

strings
:
wstring mystring = L;

15

wstring str1 (L"");


wcout << str1 << endl;


wstring output stream

_cputws(str1.c_str());

C string:

wide
characters

wchar_t buffer [100] = {97};

_cgetws(buffer);
_cputws(buffer);
wstring str2(buffer);

16

Linux,
UTF8,
wide string


UTF8,

17

string C++ ( C
)
, C


:
printf, sprintf
scanf, sscanf

18





:
int hits = 33;
long total = 1000;
printf( We scored %d out of %ld points.\nThis is %.2f%%.\n,
hits, total, 100.f*hits/(float)total );

:
We scored 33 out of 1000 points.
This is 3.30%.
19

printf( We scored %d out of %ld points.\nThis is %.2f%%.\n,


hits, total, 100.f*hits/(float)total );
1 2

% :
%d : (decimal)
%ld: (long decimal)
%f: float ( double)
: http://www.cplusplus.com/reference/cstdio/printf/
%%: %
%.2f: 2
20




printf
char name[80];
int age;
int read = scanf (name: %s, %d ",name, &age);


,


21

#define STATUS_CODE_PARSING -1
#define STATUS_CODE_OK 0
int parseVec2(float *v, char * text) {
if (!text)
return STATUS_CODE_PARSING;
if ( scanf(text, "\(%f%*[ ,\t]%f\)", &(v[0]), &(v[1])) < 2 )
return

STATUS_CODE_PARSING;

else
return

STATUS_CODE_OK;

22

scanf(text, "\(%f%*[ ,\t]%f\)", &(v[0]), &(v[1]))


\( , \) : ( )

%f :

%* : ( )

%[] :

:
(0.04 1.3)
(200, 40.45)
( 1.4

50)

(-200, 50)
23


/
(..
parsing )
( ),
(
)

24