CS计算机代考程序代写 Java c++ CS246-F20-03-IntroToC++

CS246-F20-03-IntroToC++

Lecture 3.2

• C++ strings

CS246

Character strings in CS246

#include

char * s1;
char s2[];

#include

string s;

C strings are an abomination
• As you know, character strings in C are implemented as an array

of chars with an extra “null terminating” char at the end
– Even using the string.h API, they are easy to get wrong accidentally
– Their use is also one of the biggest sources of security vulnerabilities
– But if you are restricted to using C, they are unavoidable L

• Good news: There is a string class in C++ that is intuitive,
easy to use, much less error prone, and (as long as you program
responsibly) won’t cause security vulnerabilities
– #include good J
– #include bad L

• DO NOT use char* or string.h in CS246 unless told to

Some useful bits of the string API
+ string catenation
==, <=, <, etc. comparison based on standard lexical ordering s.length() number of chars in s s.at(i) returns ith char (checked access) s[i] returns ith char (unchecked access) s.c_str() returns the C-style char* equivalent of s s.substr(i,n) substring of n characters starting at index i s.substr(i) substr starting at index i and ending at the end of s s.find(str, pos) return first index of str in s starting at or after pos ** s.rfind(str, pos) return last index of str in s starting at or before pos ** s.replace(...) replace chars within s s.insert(...) insert chars within s See also http://www.cplusplus.com/reference/string/ ** returns -1 if str is not found (technically, that's string::npos) #include // char vs. string
#include
using namespace std;
int main (int argc, char* argv[]){

string tree = “pine”;
string fruit = “apple”;

cout << tree + "apple" << endl; string f = tree + fruit; if (f == "pineapple") { cout << "yup, the same" << endl; } int n = f.length(); cout << "Beyond the " << f[0] << f.at(4) << f.substr(n-2) << endl; // Output: Beyond the pale // Beware, this "+" performs integer addition cout << "Beyond the " << f[0] + f.at(4) << endl; // Output: Beyond the 209 } Because 209 == 112 + 97 char vs. string • You can access individual characters of a string either – unchecked like an array access e.g., s[2], or – checked via the at method e.g., s.at(2) In either case, what you get is a single char, rather than a string of length 1. [Accessing vector elements is similar, as we'll see] • chars have a dual nature: they're both characters + integers: – When you use the "+" operator on chars, it performs integer addition, not string catenation. – … but when you print a char, you get the character not the integer. 'a' vs. "a" • They are very different kinds of entities! – 'a' is a single character (char), represented internally as a 1-byte "integer" – "a" is string value of length 1 • s is an instance of the class string; as we'll see, it takes a more storage than a single integer • However, for our purposes, don't worry about the storage costs; just "do the right thing" char c = 'a'; string s = "a"; #include
#include

using namespace std;

int main (int argc, char* argv[]) {
cout << "m" << endl; // m cout << 'm' << endl; // m cout << (int) 'm' << endl; // 109 string s1 = "m"; string s2 = "g"; cout << s1 + s2 << endl // mg cout << "m" + "g" << endl; // **error cout << (string)"m" + (string)"g" << endl; // mg cout << (string) "m" + 'g' << endl; // mg cout << 'm' + 'g' << endl; // 212(=109+103) } ** C++ converts the explicit string values to a char* by default (i.e., not string), and char* doesn't support "+" s == t ?? • The C++ string class defines the equality operator "==" to work in the way you would expect – That is, s==t is true iff s and t have the same string value (same characters in same order) – Not like Java (which does this "wrong") // This is Java code String s = "Hello"; String t = "Hello"; if (s == t) { // unclear if this is true … } if (s.equals(t)) { // this is always true, tho … } End CS246