<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C++ Classes :: CS3 course</title><link>https://robark.gitlab.io/c++_classes/index.html</link><description>C++ Classes String, Streams, Vector, Map</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 26 May 2021 22:40:29 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/c++_classes/index.xml" rel="self" type="application/rss+xml"/><item><title>String</title><link>https://robark.gitlab.io/c++_classes/string/index.html</link><pubDate>Wed, 12 May 2021 18:16:18 +0000</pubDate><guid>https://robark.gitlab.io/c++_classes/string/index.html</guid><description>Lesson 6: Functions continued and String introduction Palindrome
#include &lt;iostream&gt; using namespace std; bool palin(string word){ string rw(""); //sol #1 for (int x=0; x&lt;word.size();x++) { rw = word[x] + rw; // ex: home= h, oh, moh, emoh } //sol #2 /* for (int x=word.size()-1; x&gt;=0 ;x--) { rw = rw + word[x]; // ex: home= e, em, emo, emoh } */ return (rw==word); //if (rw==word) // return true; //return false; } int main(){ string w; cin &gt;&gt; w; if (palin(w)) { cout&lt;&lt;w&lt;&lt; " is a palindrome"&lt;&lt;endl; } else { cout&lt;&lt;w&lt;&lt; " is NOT a palindrome"&lt;&lt;endl; } return 0; } Strings methods:</description></item><item><title>Vectors Intro</title><link>https://robark.gitlab.io/c++_classes/vector1/index.html</link><pubDate>Thu, 13 May 2021 21:46:03 +0000</pubDate><guid>https://robark.gitlab.io/c++_classes/vector1/index.html</guid><description>Strings continued and Vectors introduction Replace substring
#include &lt;iostream&gt; using namespace std; //this program will replace a substring in a user input string //this one works when the new string has the old string in it int main(){ string s; string ow; string nw; cout &lt;&lt; "Enter a sentence: "; getline(cin, s); cout &lt;&lt; "Enter a word to delete: "; getline(cin, ow); cout &lt;&lt; "Enter another word which replaces the deleted word: "; getline(cin, nw); size_t pos(0); while ( (pos = s.find(ow, pos)) != string::npos ) { s.replace( pos, ow.size(), nw); pos = pos + nw.size(); } cout &lt;&lt; s &lt;&lt; endl; return 0; } Vector initialization</description></item><item><title>Vector Methods</title><link>https://robark.gitlab.io/c++_classes/vector2/index.html</link><pubDate>Fri, 14 May 2021 23:11:03 +0000</pubDate><guid>https://robark.gitlab.io/c++_classes/vector2/index.html</guid><description>Vector Methods Erasing from a vector properly
#include &lt;vector&gt; #include &lt;iostream&gt; using namespace std; void printvec(vector &lt;int&gt; v){ for (unsigned int i = 0; i &lt; v.size(); i++) cout &lt;&lt; v[i] &lt;&lt; ' '; cout &lt;&lt; endl; return; } void setvec(vector &lt;int&gt; &amp;v){ v.clear(); for (int i = 0; i &lt; 10; i++) { v.push_back(i); } } int main() { //create vector vector &lt;int&gt; v; vector &lt;int&gt; a; setvec(v); printvec(v); //remove items from vector CORRECT way (iterating backwards) cout&lt;&lt;"correct"&lt;&lt;endl; // unsigned int seg faults as i-- on last iteration = -1 for (int i = v.size()-1 ; i &gt;= 0; i--) { if (v[i] &lt; 5) { v.erase(v.begin() + i); } } printvec(v); //reset vector setvec(v); //remove items from vector the WRONG way (iterating forwards) cout&lt;&lt;"wrong"&lt;&lt;endl; for (unsigned int i = 0; i &lt; v.size();i++) { if (v[i] &lt; 5) { v.erase(v.begin() + i); } } printvec(v); setvec(v); //alternate method to create new vector with items to keep (not in place) cout&lt;&lt;"correct but not in place"&lt;&lt;endl; for (unsigned int i = 0; i &lt; v.size();i++) { if (v[i] &gt;= 5) { a.push_back(v[i]); } } printvec(a); return 0; } Vector order</description></item><item><title>2D Vectors</title><link>https://robark.gitlab.io/c++_classes/vector3/index.html</link><pubDate>Tue, 18 May 2021 22:59:18 +0000</pubDate><guid>https://robark.gitlab.io/c++_classes/vector3/index.html</guid><description>2D Vectors #include &lt;vector&gt; #include &lt;iostream&gt; using namespace std; //create row vectors and push_back into main vector //alternate method using push_back /* int main() { int c=0; vector&lt; vector&lt;int&gt; &gt; v1; for (int i=0; i&lt;3; i++) { vector&lt;int&gt; v2; for (int j=0; j&lt;4; j++) { v2.push_back(++c); } v1.push_back(v2); } //print 2d vector for(unsigned i=0; i&lt;v1.size(); i++) { for (unsigned j=0; j&lt;v1[i].size(); j++) { cout &lt;&lt; v1[i][j] &lt;&lt; " "; } cout &lt;&lt; endl; } return 0; } */ // alternate method initializing size then using assignment int main() { // initialize size then assign elements int c=0; vector&lt; vector&lt;int&gt; &gt; v1 (3, vector &lt;int&gt; (4)); for(unsigned i=0; i&lt;v1.size(); i++) { for (unsigned j=0; j&lt;v1[i].size(); j++) { v1[i][j]=++c; } } for(unsigned i=0; i&lt;v1.size(); i++) { for (unsigned j=0; j&lt;v1[i].size(); j++) { cout &lt;&lt; v1[i][j] &lt;&lt; " "; } cout &lt;&lt; endl; } return 0; } Sorting rows by row sum</description></item><item><title>Maps</title><link>https://robark.gitlab.io/c++_classes/maps/index.html</link><pubDate>Tue, 25 May 2021 21:50:08 +0000</pubDate><guid>https://robark.gitlab.io/c++_classes/maps/index.html</guid><description>Maps Note: Map iterators I made a mistake in the video.
vector and string use random access iterators. So &lt; is valid BUT map uses bidirectional iterators. unordered_map uses forward iterators.
#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;unordered_map&gt; //c++11 using namespace std; int main(){ // key value unordered_map &lt; string, int &gt; nameage; nameage["bob"] = 23; // add key/value pairs nameage["sarah"]= 21; nameage["bob"] = 5; //overwrite //nameage["bill"] = 8; nameage.erase("sarah"); //three ways to iterate // //1 //map&lt;string,int&gt;::iterator i; //for (i=nameage.begin(); i!=nameage.end(); i++) for (auto i=nameage.begin(); i!=nameage.end(); i++) //cannot use &lt; cout&lt;&lt; i-&gt;first &lt;&lt; ": " &lt;&lt; i-&gt;second &lt;&lt; endl; // key value //2 //c++11 for (auto x: nameage) cout &lt;&lt; x.first&lt;&lt; ": "&lt;&lt; x.second &lt;&lt; endl; //3 //c++17 use g++ -std=c++17 for (auto [k, v]: nameage ) cout &lt;&lt; k &lt;&lt; ": " &lt;&lt; v &lt;&lt;endl; //cout&lt;&lt;nameage["bill"]&lt;&lt;endl; //returns value 0 but also adds bill,0 to map //cout&lt;&lt;nameage.at("bill")&lt;&lt;endl; //fails. Does not add default values //does key exist if (nameage.find("bill") != nameage.end()) { cout &lt;&lt; nameage["bill"] &lt;&lt; endl; } else { cout&lt;&lt; "bill is not a key in the map"&lt;&lt;endl; } return 0; }</description></item><item><title>More Maps</title><link>https://robark.gitlab.io/c++_classes/maps2/index.html</link><pubDate>Wed, 26 May 2021 22:40:29 +0000</pubDate><guid>https://robark.gitlab.io/c++_classes/maps2/index.html</guid><description>More on Maps #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;unordered_map&gt; //c++11 #include &lt;map&gt; using namespace std; int main() { // key value unordered_map &lt; string, int &gt; na; //map &lt; string, int &gt; na; na["bob"] = 23; // add key/value pairs na["sarah"]= 21; na["bob"] = 5; //overwrite na["bill"] = 8; na["adam"]= 200; na["zion"]= 3; na["cathy"]=55; na.erase("sarah"); //three ways to iterate // //1 //map&lt;string,int&gt;::const_iterator i; //for (i=na.begin(); i!=na.end(); i++) for (auto i=na.begin(); i!=na.end(); i++) cout&lt;&lt; i-&gt;first &lt;&lt; ": " &lt;&lt; i-&gt;second &lt;&lt; endl; // key value cout&lt;&lt;endl; /* //if container is a map (not unordered_map) //we can iterate backwards for (auto i=na.rbegin(); i!=na.rend(); i++) cout&lt;&lt; i-&gt;first &lt;&lt; ": " &lt;&lt; i-&gt;second &lt;&lt; endl; */ cout&lt;&lt;endl; na["sarah"]=11; //2 //c++11 for (auto x: na) //auto &amp;x and auto const &amp;x { cout &lt;&lt; x.first&lt;&lt; ": "&lt;&lt; x.second &lt;&lt; endl; if (x.first=="sarah") na["sarah"]=22; } cout&lt;&lt;endl; /* //3 //c++17 for (auto [k, v]: na ) cout &lt;&lt; k &lt;&lt; ": " &lt;&lt; v &lt;&lt;endl; */ //cout&lt;&lt;na["bill"]&lt;&lt;endl; //returns value 0 but also adds bill,0 to map (na["bill]=0) //cout&lt;&lt;na.at("bill")&lt;&lt;endl; //fails //does key bob exist if (na.find("bill") != na.end()) { cout &lt;&lt; "found bill "&lt;&lt;na["bill"] &lt;&lt; endl; } else { cout&lt;&lt; "bill is not a key in the map"&lt;&lt;endl; } //alternate find //if we need to store iterator location to access pair unordered_map&lt;string, int&gt;::iterator p; //map&lt;string, int&gt;::iterator p; if ((p=na.find("sarah"))!=na.end()) { cout&lt;&lt; "found "&lt;&lt; p-&gt;first &lt;&lt;" "&lt;&lt; p-&gt;second &lt;&lt;endl; } //second alternate find with easier syntax auto q=na.find("sarah"); if (q != na.end()) { cout&lt;&lt; "found "&lt;&lt; q-&gt;first &lt;&lt;" "&lt;&lt; q-&gt;second &lt;&lt;endl; } return 0; }</description></item></channel></rss>