[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"sanity-xt35d6Jr-FVKwAtecDMSC5hwOTdjSSjld4VnfcY4TBc":3,"sanity-Azu3iJQZa8CjbwY6R2va1FyK9CBlmA9TRD3VZqcABKg":819},{"data":4,"sourceMap":-1},{"latestPodcast":5,"latestReleases":14,"post":39,"recent":794},[6],{"_id":7,"publishedAt":8,"slug":9,"sponsored":12,"title":13},"f83eb5f0-1237-487f-84d8-f7abf2318c39","2026-06-25T07:40:00.000Z",{"_type":10,"current":11},"slug","code-isnt-causing-your-production-failures",null,"Code isn’t the only thing causing your production failures",[15,21,27,33],{"_id":16,"publishedAt":17,"slug":18,"title":20},"eb5b66eb-9410-4329-83bb-22bbff39402a","2026-04-28T13:00:00.000Z",{"_type":10,"current":19},"turn-scattered-knowledge-into-trusted-intelligence","Turning scattered knowledge into trusted intelligence: Stack Internal 2026.3",{"_id":22,"publishedAt":23,"slug":24,"title":26},"369c2401-b62e-4a37-8ff8-bf603023ecad","2026-03-02T15:03:00.988Z",{"_type":10,"current":25},"what-s-new-at-stack-overflow-march-2026","What’s new at Stack Overflow: March 2026",{"_id":28,"publishedAt":29,"slug":30,"title":32},"5e9053a4-07ea-447c-91ea-29e0b6228537","2026-02-02T15:00:00.000Z",{"_type":10,"current":31},"what-s-new-at-stack-overflow-february-2026","What’s new at Stack Overflow: February 2026",{"_id":34,"publishedAt":35,"slug":36,"title":38},"a1b538eb-a8a6-46d0-80a1-ac70ec9bb935","2026-01-05T10:00:00.000-05:00",{"_type":10,"current":37},"what-s-new-at-stack-overflow-january-2026","What’s new at Stack Overflow: January 2026",{"_createdAt":40,"_id":41,"_rev":42,"_type":43,"_updatedAt":44,"author":45,"body":61,"comments":750,"dateUrl":751,"excerpt":227,"image":752,"legacyBody":755,"product":12,"publishedAt":758,"slug":759,"sponsored":12,"tags":761,"title":793,"visible":750},"2023-05-25T09:39:12Z","wp-post-13587","XzVcPPDfPNmxxXS3zvDpoM","blogPost","2025-10-31T17:26:43Z",[46],{"_createdAt":47,"_id":48,"_rev":49,"_type":50,"_updatedAt":51,"avatar":52,"employee":57,"name":58,"slug":59},"2023-05-23T16:27:18Z","wp-author-240","dgl3SCUzppW3U2LvCoP35A","blogAuthor","2023-06-20T15:05:08Z",{"_type":53,"asset":54},"image",{"_ref":55,"_type":56},"image-62c5b2f98b1dd95c24a843144355d39bc71ae218-1024x1024-jpg","reference","none","Sonny Li",{"current":60},"sli",[62,115,132,137,145,149,157,160,176,199,222,228,243,246,278,281,343,346,354,369,374,437,452,458,466,469,485,488,496,499,507,510,518,521,529,532,540,543,559,574,579,619,622,630,633,649,652,676,679,695,720,738,742],{"_key":63,"_type":64,"children":65,"markDefs":108,"style":114},"4a16717aaca3","block",[66,72,77,81,85,89,94,98,101,105],{"_key":67,"_type":68,"marks":69,"text":71},"4a16717aaca30","span",[70],"em","Mariel Frank and Sonny Li, authors of Codecademy’s ",{"_key":73,"_type":68,"marks":74,"text":76},"4a16717aaca31",[75,70],"a095baff0efd","Learn C++",{"_key":78,"_type":68,"marks":79,"text":80},"4a16717aaca32",[70]," course, recently got a chance to interview with Dr. Bjarne Stroustrup, the creator of C++.",{"_key":82,"_type":68,"marks":83,"text":84},"4a16717aaca33",[],"\n\n",{"_key":86,"_type":68,"marks":87,"text":88},"4a16717aaca34",[70],"As part of the interview, he answered the highest voted C++ questions on Stack Overflow. While ",{"_key":90,"_type":68,"marks":91,"text":93},"4a16717aaca35",[92,70],"f23a6676b417","the whole interview",{"_key":95,"_type":68,"marks":96,"text":97},"4a16717aaca36",[70]," is worth a read, Codecademy has generously allowed us to republish the Q&A portion of the interview.",{"_key":99,"_type":68,"marks":100,"text":84},"4a16717aaca37",[],{"_key":102,"_type":68,"marks":103,"text":104},"4a16717aaca38",[70],"If you ever wondered if an answer on Stack Overflow was definitive, here’s about the closest you’ll get to certain (though we expect somebody to disagree).",{"_key":106,"_type":68,"marks":107,"text":84},"4a16717aaca39",[],[109,112],{"_key":75,"_type":110,"href":111,"reference":12},"link","https://www.codecademy.com/learn/learn-c-plus-plus",{"_key":92,"_type":110,"href":113,"reference":12},"https://news.codecademy.com/bjarne-stroustrup-interview/","normal",{"_key":116,"_type":64,"children":117,"markDefs":128,"style":131},"5aad2c8aaad5",[118,123],{"_key":119,"_type":68,"marks":120,"text":122},"5aad2c8aaad50",[121],"strong","🥇 ",{"_key":124,"_type":68,"marks":125,"text":127},"5aad2c8aaad51",[126,121],"91b5bcb23b90","Why is processing a sorted array faster than processing an unsorted array?",[129],{"_key":126,"_type":110,"href":130,"reference":12},"https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-processing-an-unsorted-array","h3",{"_key":133,"_type":53,"alt":127,"asset":134,"caption":136,"markDefs":12},"153b1f6d20e9",{"_ref":135,"_type":56},"image-20ab2f1e260552349ede4adf02ea002e6a25eaba-1448x414-png"," Note: This question is the #1 highest voted question on Stack Overflow of all time. 😱 ",{"_key":138,"_type":64,"children":139,"markDefs":144,"style":114},"f23f07b7b762",[140],{"_key":141,"_type":68,"marks":142,"text":143},"f23f07b7b7620",[],"That sounds like an interview question. Is it true? How would you know? It is a bad idea to answer questions about efficiency without first doing some measurements, so it is important to know how to measure.\n\nSo, I tried with a vector of a million integers and got:",[],{"_key":146,"_type":147,"code":148,"markDefs":12},"469bd3d8147e","code","Already sorted    32995 milliseconds\nShuffled          125944 milliseconds\n\nAlready sorted    18610 milliseconds\nShuffled          133304 milliseconds\n\nAlready sorted    17942 milliseconds\nShuffled          107858 milliseconds\n",{"_key":150,"_type":64,"children":151,"markDefs":156,"style":114},"8c79b486ac9f",[152],{"_key":153,"_type":68,"marks":154,"text":155},"8c79b486ac9f0",[],"I ran that a few times to be sure. Yes, the phenomenon is real. My key code was:",[],{"_key":158,"_type":147,"code":159,"markDefs":12},"d6fd6146dbaa","void run(vector\u003Cint>& v, const string& label)\n{\n    auto t0 = system_clock::now();\n    sort(v.begin(), v.end());\n    auto t1 = system_clock::now();\n    cout \u003C\u003C label \n         \u003C\u003C duration_cast\u003Cmicroseconds>(t1 — t0).count() \n         \u003C\u003C \" milliseconds\\n\";\n}\n\nvoid tst()\n{\n    vector\u003Cint> v(1'000'000);\n    iota(v.begin(), v.end(), 0);\n    run(v, \"already sorted \");\n    std::shuffle(v.begin(), v.end(), std::mt19937{ std::random_device{}() });\n    run(v, \"shuffled    \");\n}\n",{"_key":161,"_type":64,"children":162,"markDefs":175,"style":114},"5069b8f20920",[163,167,171],{"_key":164,"_type":68,"marks":165,"text":166},"5069b8f209200",[],"At least the phenomenon is real with this compiler, standard library, and optimizer settings. Different implementations can and do give different answers. In fact, someone did do a more systematic study (a quick web search will find it) and most implementations show that effect.\n\nOne reason is branch prediction: the key operation in the sort algorithm is ",{"_key":168,"_type":68,"marks":169,"text":170},"5069b8f209201",[147],"“if(v[i] \u003C pivot]) …”",{"_key":172,"_type":68,"marks":173,"text":174},"5069b8f209202",[]," or equivalent. For a sorted sequence that test is always true whereas, for a random sequence, the branch chosen varies randomly.\n\nAnother reason is that when the vector is already sorted, we never need to move elements to their correct position. The effect of these little details is the factor of five or six that we saw.\n\nQuicksort (and sorting in general) is a complex study that has attracted some of the greatest minds of computer science. A good sort function is a result of both choosing a good algorithm and paying attention to hardware performance in its implementation.\n\nIf you want to write efficient code, you need to know a bit about machine architecture.\n\n",[],{"_key":177,"_type":64,"children":178,"markDefs":196,"style":114},"7899d9ab0410",[179,183,187,192],{"_key":180,"_type":68,"marks":181,"text":182},"7899d9ab04100",[],"-------------------------------------------------------------------------------------------------------------------\n",{"_key":184,"_type":68,"marks":185,"text":186},"7899d9ab04101",[70],"Pardon the interruption. Just here to say, the ",{"_key":188,"_type":68,"marks":189,"text":191},"7899d9ab04102",[70,190],"9c1b40279be4","Stack Overflow Podcast is back",{"_key":193,"_type":68,"marks":194,"text":195},"7899d9ab04103",[70],"! Come check us out and hear an interview with our new CEO.",[197],{"_key":190,"_type":110,"href":198,"reference":12},"https://stackoverflow.blog/2019/10/15/the-stack-overflow-podcast-is-back/",{"_key":200,"_type":64,"children":201,"markDefs":219,"style":131},"562b9b0e576c",[202,206,211,215],{"_key":203,"_type":68,"marks":204,"text":205},"562b9b0e576c0",[121],"🥈 ",{"_key":207,"_type":68,"marks":208,"text":210},"562b9b0e576c1",[209,121],"eef27d8e9679","What is the ",{"_key":212,"_type":68,"marks":213,"text":214},"562b9b0e576c2",[209,121,147],"-->",{"_key":216,"_type":68,"marks":217,"text":218},"562b9b0e576c3",[209,121]," operator in C++?",[220],{"_key":209,"_type":110,"href":221,"reference":12},"https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c",{"_key":223,"_type":53,"alt":224,"asset":225,"caption":227,"markDefs":12},"efdd9a6e0171","What-is-the------operator-in-C---",{"_ref":226,"_type":56},"image-5e383d30eee8bd1249459e92aff9fce7b2111c70-1268x408-png","",{"_key":229,"_type":64,"children":230,"markDefs":242,"style":114},"a3956e2a08c9",[231,235,238],{"_key":232,"_type":68,"marks":233,"text":234},"a3956e2a08c90",[],"That’s an old trick question. There is no ",{"_key":236,"_type":68,"marks":237,"text":214},"a3956e2a08c91",[147],{"_key":239,"_type":68,"marks":240,"text":241},"a3956e2a08c92",[]," operator in C++. Consider:",[],{"_key":244,"_type":147,"code":245,"markDefs":12},"d4a439ffcca9","if (p-->m == 0) f(p);\n",{"_key":247,"_type":64,"children":248,"markDefs":277,"style":114},"93b8c5bfc666",[249,253,257,261,265,269,273],{"_key":250,"_type":68,"marks":251,"text":252},"93b8c5bfc6660",[],"It certainly looks as if there is a",{"_key":254,"_type":68,"marks":255,"text":256},"93b8c5bfc6661",[147]," —->",{"_key":258,"_type":68,"marks":259,"text":260},"93b8c5bfc6662",[]," operator and by suitable declaring ",{"_key":262,"_type":68,"marks":263,"text":264},"93b8c5bfc6663",[147],"p",{"_key":266,"_type":68,"marks":267,"text":268},"93b8c5bfc6664",[]," and ",{"_key":270,"_type":68,"marks":271,"text":272},"93b8c5bfc6665",[147],"m",{"_key":274,"_type":68,"marks":275,"text":276},"93b8c5bfc6666",[],", you can even get that to compile and run:",[],{"_key":279,"_type":147,"code":280,"markDefs":12},"905808eb4343","int p = 2;\nint m = 0;\nif (p-->m == 0) f(p);\n",{"_key":282,"_type":64,"children":283,"markDefs":342,"style":114},"aaa0a234cd7b",[284,288,292,296,299,303,307,311,315,319,323,327,331,334,338],{"_key":285,"_type":68,"marks":286,"text":287},"aaa0a234cd7b0",[],"That means: see if ",{"_key":289,"_type":68,"marks":290,"text":291},"aaa0a234cd7b1",[147],"p--",{"_key":293,"_type":68,"marks":294,"text":295},"aaa0a234cd7b2",[]," is greater than ",{"_key":297,"_type":68,"marks":298,"text":272},"aaa0a234cd7b3",[147],{"_key":300,"_type":68,"marks":301,"text":302},"aaa0a234cd7b4",[]," (it is), and then compare the result (",{"_key":304,"_type":68,"marks":305,"text":306},"aaa0a234cd7b5",[147],"true",{"_key":308,"_type":68,"marks":309,"text":310},"aaa0a234cd7b6",[],") to ",{"_key":312,"_type":68,"marks":313,"text":314},"aaa0a234cd7b7",[147],"0",{"_key":316,"_type":68,"marks":317,"text":318},"aaa0a234cd7b8",[],". Well ",{"_key":320,"_type":68,"marks":321,"text":322},"aaa0a234cd7b9",[147],"true != 0",{"_key":324,"_type":68,"marks":325,"text":326},"aaa0a234cd7b10",[],", so the result is ",{"_key":328,"_type":68,"marks":329,"text":330},"aaa0a234cd7b11",[147],"false",{"_key":332,"_type":68,"marks":333,"text":268},"aaa0a234cd7b12",[],{"_key":335,"_type":68,"marks":336,"text":337},"aaa0a234cd7b13",[147],"f()",{"_key":339,"_type":68,"marks":340,"text":341},"aaa0a234cd7b14",[]," is not called. In other words:",[],{"_key":344,"_type":147,"code":345,"markDefs":12},"0b4d176faa52","if ((p--) > m == 0) f(p);\n",{"_key":347,"_type":64,"children":348,"markDefs":353,"style":114},"3c307e6a11b3",[349],{"_key":350,"_type":68,"marks":351,"text":352},"3c307e6a11b30",[],"Please don’t waste too much time on such questions. They have been popular for befuddling novices since before C++ was invented.\n\n",[],{"_key":355,"_type":64,"children":356,"markDefs":366,"style":131},"fcfed12abf37",[357,361],{"_key":358,"_type":68,"marks":359,"text":360},"fcfed12abf370",[121],"🥉 ",{"_key":362,"_type":68,"marks":363,"text":365},"fcfed12abf371",[364,121],"cc7c53219d30","The Definitive C++ Book Guide and List",[367],{"_key":364,"_type":110,"href":368,"reference":12},"https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list",{"_key":370,"_type":53,"alt":371,"asset":372,"caption":227,"markDefs":12},"eb88130e0802","The-Definitive-C---Book-Guide-and-List",{"_ref":373,"_type":56},"image-339da504a0801a6315f64673d8686e96104e1ef2-1256x348-png",{"_key":375,"_type":64,"children":376,"markDefs":426,"style":114},"1118617d343a",[377,381,386,390,395,399,404,408,413,417,422],{"_key":378,"_type":68,"marks":379,"text":380},"1118617d343a0",[],"Unfortunately, there is no definite C++ book list. There can’t be one. Not everyone needs the same information, not everyone has the same background, and C++ best practices are evolving.\n\nI did a search of the web and found a bewildering set of suggestions. Many were seriously outdated and some were bad from the start. A novice looking for a good book without guidance will be very confused!\n\nYou do need a book because the techniques that make C++ effective are not easily picked up from a few blogs on specific topics—and of course blogs also suffer from mistakes, being dated, and poor explanations. Often, they also focus on advanced new stuff and ignore the essential fundamentals.\n\nI recommend my ",{"_key":382,"_type":68,"marks":383,"text":385},"1118617d343a1",[384,70],"0ce5837bf34d","Programming: Principles and Practice Using C++ (2nd Edition)",{"_key":387,"_type":68,"marks":388,"text":389},"1118617d343a2",[]," for people just beginning to learn to program, and ",{"_key":391,"_type":68,"marks":392,"text":394},"1118617d343a3",[393,70],"6ccf12f903b2","A Tour of C++ (2nd Edition)",{"_key":396,"_type":68,"marks":397,"text":398},"1118617d343a4",[]," for people who are already programmers and need to know about modern C++. People with a strong mathematical background can start with Peter Gottschling‘s ",{"_key":400,"_type":68,"marks":401,"text":403},"1118617d343a5",[402,70],"3329a8031000","Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers",{"_key":405,"_type":68,"marks":406,"text":407},"1118617d343a6",[],".\n\nOnce you start using C++ for real, you need a set of guidelines to distinguish what can be done and what is good practice. For that, I recommend the ",{"_key":409,"_type":68,"marks":410,"text":412},"1118617d343a7",[411,70],"4a9ec3bb2557","C++ Core Guidelines on GitHub",{"_key":414,"_type":68,"marks":415,"text":416},"1118617d343a8",[],".\n\nFor good brief explanations of individual language features and standard-library functions, I recommend ",{"_key":418,"_type":68,"marks":419,"text":421},"1118617d343a9",[420],"7737865167ac","www.cppreference.com",{"_key":423,"_type":68,"marks":424,"text":425},"1118617d343a10",[],".\n\n",[427,429,431,433,435],{"_key":384,"_type":110,"href":428,"reference":12},"https://amzn.to/2Ob6lcH",{"_key":393,"_type":110,"href":430,"reference":12},"https://amzn.to/2LGYthA",{"_key":402,"_type":110,"href":432,"reference":12},"https://amzn.to/2LEFCDS",{"_key":411,"_type":110,"href":434,"reference":12},"https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md",{"_key":420,"_type":110,"href":436,"reference":12},"http://www.cppreference.com/",{"_key":438,"_type":64,"children":439,"markDefs":449,"style":131},"6dab975d5184",[440,444],{"_key":441,"_type":68,"marks":442,"text":443},"6dab975d51840",[121],"#4. ",{"_key":445,"_type":68,"marks":446,"text":448},"6dab975d51841",[447,121],"3a4bf3d39b78","What are the differences between a pointer variable and a reference variable in C++?",[450],{"_key":447,"_type":110,"href":451,"reference":12},"https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in",{"_key":453,"_type":53,"alt":454,"asset":455,"caption":457,"markDefs":12},"6c5254daf2e6","What-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in-C---",{"_ref":456,"_type":56},"image-48f252f0180e730a49a79892e6aa72eb2e1ee02d-1284x404-png","To learn more about references and pointers, check out Learn C++. 😊",{"_key":459,"_type":64,"children":460,"markDefs":465,"style":114},"4151f4fc2708",[461],{"_key":462,"_type":68,"marks":463,"text":464},"4151f4fc27080",[],"Both are represented in memory as a machine address. The difference is in their use.\n\nTo initialize a pointer, you give it the address of an object:\n",[],{"_key":467,"_type":147,"code":468,"markDefs":12},"077c07667f89","int x = 7;\nint* p1 = &x;\nint* p2 = new int{9};\n",{"_key":470,"_type":64,"children":471,"markDefs":484,"style":114},"5c81b73058db",[472,476,480],{"_key":473,"_type":68,"marks":474,"text":475},"5c81b73058db0",[],"To read and write through a pointer, we use the dereference operator (",{"_key":477,"_type":68,"marks":478,"text":479},"5c81b73058db1",[147],"*",{"_key":481,"_type":68,"marks":482,"text":483},"5c81b73058db2",[],"):",[],{"_key":486,"_type":147,"code":487,"markDefs":12},"86bde35f7235","*p1 = 9;       // write through p1\nint y = *p2;   // read through p2\n",{"_key":489,"_type":64,"children":490,"markDefs":495,"style":114},"380107dad6e6",[491],{"_key":492,"_type":68,"marks":493,"text":494},"380107dad6e60",[],"When we assign one pointer to another, they will both point to the same object:",[],{"_key":497,"_type":147,"code":498,"markDefs":12},"54c52c92f9f8","p1 = p2;       // now p1 p2 both point to the int with the value 9\n*p2 = 99;      // write 99 through p2\nint z = *p1;   // reading through p1, z becomes 99 (not 9)\n",{"_key":500,"_type":64,"children":501,"markDefs":506,"style":114},"8b48533f2384",[502],{"_key":503,"_type":68,"marks":504,"text":505},"8b48533f23840",[],"Note that a pointer can point to different objects during its lifetime. That’s a major difference from references. A reference is bound to an object when it is created and cannot be made to refer to another.\n\nFor references, dereferencing is implicit. You initialize a reference with an object and the reference takes its address.",[],{"_key":508,"_type":147,"code":509,"markDefs":12},"733283b7a93e","int x = 7;\nint& r1 = x;\nint& r2 = *new int{9};\n",{"_key":511,"_type":64,"children":512,"markDefs":517,"style":114},"849bfafa47bc",[513],{"_key":514,"_type":68,"marks":515,"text":516},"849bfafa47bc0",[],"Operator new returns a pointer, so I had to dereference it before assigning using it to initialize the reference.\n\nTo read and write through a reference, we just use the reference’s name (no explicit dereferencing):",[],{"_key":519,"_type":147,"code":520,"markDefs":12},"ed903c5ba7e8","r1 = 9;        // write through r1\nint y = r2;    // read through r2\n",{"_key":522,"_type":64,"children":523,"markDefs":528,"style":114},"f6ea79e05756",[524],{"_key":525,"_type":68,"marks":526,"text":527},"f6ea79e057560",[],"When we assign one reference to another, the value referred to will be copied:",[],{"_key":530,"_type":147,"code":531,"markDefs":12},"aecc6b260127","r1 = r2;       // now p1 and p2 both have the value 9\nr1 = 99;       // write 99 through r1\nint z = r2;    // reading through r2, z becomes 9 (not 99)\n",{"_key":533,"_type":64,"children":534,"markDefs":539,"style":114},"a11d074a7cdb",[535],{"_key":536,"_type":68,"marks":537,"text":538},"a11d074a7cdb0",[],"Both references and pointers are frequently used as function arguments:",[],{"_key":541,"_type":147,"code":542,"markDefs":12},"c6d618168c18","void f(int* p)\n\n{\n    if (p == nullptr) return;\n    // ...\n}\n\nvoid g(int& r)\n{\n    // ...\n}\n\nint x = 7;\nf(&x);\ng(x);\n",{"_key":544,"_type":64,"children":545,"markDefs":558,"style":114},"25aacc9369fb",[546,550,554],{"_key":547,"_type":68,"marks":548,"text":549},"25aacc9369fb0",[],"A pointer can be the ",{"_key":551,"_type":68,"marks":552,"text":553},"25aacc9369fb1",[147],"nullptr",{"_key":555,"_type":68,"marks":556,"text":557},"25aacc9369fb2",[],", so we have to consider whether it points to anything. A reference can be assumed to refer to something.\n\n",[],{"_key":560,"_type":64,"children":561,"markDefs":571,"style":131},"63535c656a1b",[562,566],{"_key":563,"_type":68,"marks":564,"text":565},"63535c656a1b0",[121],"#5. ",{"_key":567,"_type":68,"marks":568,"text":570},"63535c656a1b1",[569,121],"928c28de3fa7","How do I iterate over the words of a string?",[572],{"_key":569,"_type":110,"href":573,"reference":12},"https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string",{"_key":575,"_type":53,"alt":576,"asset":577,"caption":227,"markDefs":12},"c71e25c23b0f","How-do-I-iterate-over-the-words-of-a-string-",{"_ref":578,"_type":56},"image-8dcbe12d398dbb9131173d12a0fbb6a818850293-1296x352-png",{"_key":580,"_type":64,"children":581,"markDefs":618,"style":114},"c74329e7df01",[582,586,590,594,598,602,606,610,614],{"_key":583,"_type":68,"marks":584,"text":585},"c74329e7df010",[],"Use a ",{"_key":587,"_type":68,"marks":588,"text":589},"c74329e7df011",[147],"stringstream",{"_key":591,"_type":68,"marks":592,"text":593},"c74329e7df012",[],", but how do you define “a word”? Consider ",{"_key":595,"_type":68,"marks":596,"text":597},"c74329e7df013",[121],"“Mary had a little lamb.”",{"_key":599,"_type":68,"marks":600,"text":601},"c74329e7df014",[]," Is the last word ",{"_key":603,"_type":68,"marks":604,"text":605},"c74329e7df015",[121],"“lamb”",{"_key":607,"_type":68,"marks":608,"text":609},"c74329e7df016",[]," or ",{"_key":611,"_type":68,"marks":612,"text":613},"c74329e7df017",[121],"“lamb.”",{"_key":615,"_type":68,"marks":616,"text":617},"c74329e7df018",[],"? If there is no punctuation, it is easy:",[],{"_key":620,"_type":147,"code":621,"markDefs":12},"5168912772d9","vector\u003Cstring> split(const string& s)\n{\n    stringstream ss(s);\n    vector\u003Cstring> words;\n    for (string w; ss>>w; ) words.push_back(w);\n    return words;\n}\n\nauto words = split(\"here is a simple example\");   // five words\nfor (auto& w : words) cout \u003C\u003C w \u003C\u003C '\\n';\n",{"_key":623,"_type":64,"children":624,"markDefs":629,"style":114},"2246f430ed51",[625],{"_key":626,"_type":68,"marks":627,"text":628},"2246f430ed510",[],"or just:",[],{"_key":631,"_type":147,"code":632,"markDefs":12},"2b0619329dbc","for (auto& w : split(\"here is a simple example\")) cout \u003C\u003C w \u003C\u003C '\\n';\n",{"_key":634,"_type":64,"children":635,"markDefs":648,"style":114},"5293d30f35c2",[636,640,644],{"_key":637,"_type":68,"marks":638,"text":639},"5293d30f35c20",[],"By default, the ",{"_key":641,"_type":68,"marks":642,"text":643},"5293d30f35c21",[147],">>",{"_key":645,"_type":68,"marks":646,"text":647},"5293d30f35c22",[]," operator skips whitespace. If we want arbitrary sets of delimiters, things get a bit more messy:",[],{"_key":650,"_type":147,"code":651,"markDefs":12},"7f154b71536d","template\u003Ctypename Delim>\nstring get_word(istream& ss, Delim d)\n{\n    string word;\n    for (char ch; ss.get(ch); )    // skip delimiters\n        if (!d(ch)) {\n            word.push_back(ch);\n            break;\n        }\n    for (char ch; ss.get(ch); )    // collect word\n        if (!d(ch))\n            word.push_back(ch);\n        else\n            break;\n    return word;\n}\n",{"_key":653,"_type":64,"children":654,"markDefs":675,"style":114},"89b5cc9ed52e",[655,659,663,667,671],{"_key":656,"_type":68,"marks":657,"text":658},"89b5cc9ed52e0",[],"The ",{"_key":660,"_type":68,"marks":661,"text":662},"89b5cc9ed52e1",[147],"d",{"_key":664,"_type":68,"marks":665,"text":666},"89b5cc9ed52e2",[]," is an operation telling whether a character is a delimiter and I return ",{"_key":668,"_type":68,"marks":669,"text":670},"89b5cc9ed52e3",[147],"\"\"",{"_key":672,"_type":68,"marks":673,"text":674},"89b5cc9ed52e4",[]," (the empty string) to indicate that there wasn’t a word to return.",[],{"_key":677,"_type":147,"code":678,"markDefs":12},"b61cf817ab40","vector\u003Cstring> split(const string& s, const string& delim)\n{\n    stringstream ss(s);\n    auto del = [&](char ch) { for (auto x : delim) if (x == ch) return true; return false; };\n\n    vector\u003Cstring> words;\n    for (string w; (w = get_word(ss, del))!= \"\"; ) words.push_back(w);\n    return words;\n}\n\nauto words = split(\"Now! Here is something different; or is it? \", \"!.,;? \");\nfor (auto& w : words) cout \u003C\u003C w \u003C\u003C '\\n';\n",{"_key":680,"_type":64,"children":681,"markDefs":694,"style":114},"10bc46b2def0",[682,686,690],{"_key":683,"_type":68,"marks":684,"text":685},"10bc46b2def00",[],"If you have the C++20 Range library, you don’t have to write something like this yourself but can use a ",{"_key":687,"_type":68,"marks":688,"text":689},"10bc46b2def01",[147],"split_view",{"_key":691,"_type":68,"marks":692,"text":693},"10bc46b2def02",[],".",[],{"_key":696,"_type":64,"children":697,"markDefs":717,"style":114},"fd9c089a3b58",[698,702,705,709,714],{"_key":699,"_type":68,"marks":700,"text":701},"fd9c089a3b580",[70],"Bjarne Stroustrup is a Technical Fellow and Managing Director at Morgan Stanley in New York City and a Visiting Professor at Columbia University. He's also the creator of C++.",{"_key":703,"_type":68,"marks":704,"text":84},"fd9c089a3b581",[],{"_key":706,"_type":68,"marks":707,"text":708},"fd9c089a3b582",[70],"For more information on C++20: ",{"_key":710,"_type":68,"marks":711,"text":713},"fd9c089a3b583",[712,70],"b7c4cd61ed85","https://isocpp.org",{"_key":715,"_type":68,"marks":716,"text":693},"fd9c089a3b584",[70],[718],{"_key":712,"_type":110,"href":719,"reference":12},"https://isocpp.org/",{"_key":721,"_type":64,"children":722,"markDefs":736,"style":114},"5f1b279cb864",[723,727,732],{"_key":724,"_type":68,"marks":725,"text":726},"5f1b279cb8640",[],"\n\nDone reading this awesome post? We have something fun for ya. The Stack Overflow podcast is back! Come ",{"_key":728,"_type":68,"marks":729,"text":731},"5f1b279cb8641",[730],"e7bc118ad4a8","check it out",{"_key":733,"_type":68,"marks":734,"text":735},"5f1b279cb8642",[]," or listen below.",[737],{"_key":730,"_type":110,"href":198,"reference":12},{"_key":739,"_type":740,"markDefs":12,"url":741},"ce07ea741e33","embed","https://art19.com/shows/2355b740-4531-4071-a3ab-5907a95a36d3/episodes/15ff1955-bbd7-425a-a4f7-116a30091540",{"_key":743,"_type":64,"children":744,"markDefs":749,"style":114},"17141a2910a0",[745],{"_key":746,"_type":68,"marks":747,"text":748},"17141a2910a00",[],"\n",[],true,"2019/10/11",{"_type":53,"asset":753},{"_ref":754,"_type":56},"image-245cd5e304b680a034152c7e7b6f2c15915ceab8-1103x713-jpg",{"code":756,"language":757},"\u003C!-- wp:paragraph -->\n\u003Cp>\u003Cem>Mariel Frank and Sonny Li, authors of Codecademy’s \u003C/em>\u003Ca href=\"https://www.codecademy.com/learn/learn-c-plus-plus\">\u003Cem>Learn C++\u003C/em>\u003C/a>\u003Cem> course, recently got a chance to interview with Dr. Bjarne Stroustrup, the creator of C++.&nbsp;\u003C/em>\u003Cbr>\u003Cbr>\u003Cem>As part of the interview, he answered the highest voted C++ questions on Stack Overflow. While \u003C/em>\u003Ca href=\"https://news.codecademy.com/bjarne-stroustrup-interview/\">\u003Cem>the whole interview\u003C/em>\u003C/a>\u003Cem> is worth a read, Codecademy has generously allowed us to republish the Q&amp;A portion of the interview.&nbsp;\u003C/em>\u003Cbr>\u003Cbr>\u003Cem>If you ever wondered if an answer on Stack Overflow was definitive, here’s about the closest you’ll get to certain (though we expect somebody to disagree).&nbsp;\u003C/em>\u003Cbr>\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3>\u003Cstrong>🥇 \u003C/strong>\u003Ca href=\"https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-processing-an-unsorted-array\">\u003Cstrong>Why is processing a sorted array faster than processing an unsorted array?\u003C/strong>\u003C/a>\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:image -->\n\u003Cfigure class=\"wp-block-image\">\u003Cimg src=\"https://lh4.googleusercontent.com/TxB8cQdzUzDHfh46mF0pyoxHwWtqZ2Ci8fDlp7BxCN0XTYTmuwCDHfRW0PY4PnNNhgLLahwQ4gqBwHri4AuyAihmWhZp3_Mp0Pd5EM6REM-9vCa0YXy6HhMBh2R22CSzBw-quAv4\" alt=\"Why is processing a sorted array faster than processing an unsorted array?\"/>\u003Cfigcaption> \u003Cstrong>Note:\u003C/strong> This question is the #1 highest voted question on Stack Overflow of all time. 😱 \u003C/figcaption>\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>That sounds like an interview question. Is it true? How would you know? It is a bad idea to answer questions about efficiency without first doing some measurements, so it is important to know how to measure.\u003Cbr>\u003Cbr>So, I tried with a vector of a million integers and got:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">Already sorted&nbsp;   32995 milliseconds\nShuffled&nbsp; &nbsp; &nbsp; &nbsp;   125944 milliseconds\n\nAlready sorted&nbsp;   18610 milliseconds\nShuffled&nbsp; &nbsp; &nbsp; &nbsp;   133304 milliseconds\n\nAlready sorted&nbsp;   17942 milliseconds\nShuffled&nbsp; &nbsp; &nbsp; &nbsp;   107858 milliseconds\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>I ran that a few times to be sure. Yes, the phenomenon is real. My key code was:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">void run(vector&lt;int&gt;&amp; v, const string&amp; label)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;auto t0 = system_clock::now();\n&nbsp;&nbsp;&nbsp;&nbsp;sort(v.begin(), v.end());\n&nbsp;&nbsp;&nbsp;&nbsp;auto t1 = system_clock::now();\n&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; label&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;&lt; duration_cast&lt;microseconds&gt;(t1 — t0).count()&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;&lt; \" milliseconds\\n\";\n}\n\nvoid tst()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;vector&lt;int&gt; v(1'000'000);\n&nbsp;&nbsp;&nbsp;&nbsp;iota(v.begin(), v.end(), 0);\n&nbsp;&nbsp;&nbsp;&nbsp;run(v, \"already sorted \");\n&nbsp;&nbsp;&nbsp;&nbsp;std::shuffle(v.begin(), v.end(), std::mt19937{ std::random_device{}() });\n&nbsp;&nbsp;&nbsp;&nbsp;run(v, \"shuffled&nbsp; &nbsp; \");\n}\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>At least the phenomenon is real with this compiler, standard library, and optimizer settings. Different implementations can and do give different answers. In fact, someone did do a more systematic study (a quick web search will find it) and most implementations show that effect.\u003Cbr>\u003Cbr>One reason is branch prediction: the key operation in the sort algorithm is \u003Ccode>“if(v[i] &lt; pivot]) …”\u003C/code> or equivalent. For a sorted sequence that test is always true whereas, for a random sequence, the branch chosen varies randomly.\u003Cbr>\u003Cbr>Another reason is that when the vector is already sorted, we never need to move elements to their correct position. The effect of these little details is the factor of five or six that we saw.\u003Cbr>\u003Cbr>Quicksort (and sorting in general) is a complex study that has attracted some of the greatest minds of computer science. A good sort function is a result of both choosing a good algorithm and paying attention to hardware performance in its implementation.\u003Cbr>\u003Cbr>If you want to write efficient code, you need to know a bit about machine architecture.\u003Cbr>\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>-------------------------------------------------------------------------------------------------------------------\u003Cbr>\u003Cem>Pardon the interruption. Just here to say, the \u003Ca href=\"https://stackoverflow.blog/2019/10/15/the-stack-overflow-podcast-is-back/\">Stack Overflow Podcast is back\u003C/a>! Come check us out and hear an interview with our new CEO.\u003C/em>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:html -->\n\u003Cfigure>\n\n\n\n\u003C/figure>\n\u003C!-- /wp:html -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3>\u003Cstrong>🥈 \u003C/strong>\u003Ca href=\"https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c\">\u003Cstrong>What is the \u003Ccode>--&gt;\u003C/code> operator in C++?\u003C/strong>\u003C/a>\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:image -->\n\u003Cfigure class=\"wp-block-image\">\u003Cimg src=\"https://lh5.googleusercontent.com/io8WjzU3s1sQFYu-vPC2STE-9cxQinbZ3sWFRP92bdolT7Tv3HI9cgqKRTZfw8NGMgbFSO-ysMu6PrOMb2XFbLfbypK2AMpoLAbqXg0i8owC8Kyq8LYLrtppROZNnEyJsMsH28wW\" alt=\"What-is-the------operator-in-C---\"/>\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>That’s an old trick question. There is no \u003Ccode>--&gt;\u003C/code> operator in C++. Consider:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">if (p--&gt;m == 0) f(p);\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>It certainly looks as if there is a\u003Ccode> —-&gt;\u003C/code> operator and by suitable declaring \u003Ccode>p\u003C/code> and \u003Ccode>m\u003C/code>, you can even get that to compile and run:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">int p = 2;\u003Cbr>int m = 0;\u003Cbr>if (p--&gt;m == 0) f(p);\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>That means: see if \u003Ccode>p--\u003C/code> is greater than \u003Ccode>m\u003C/code> (it is), and then compare the result (\u003Ccode>true\u003C/code>) to \u003Ccode>0\u003C/code>. Well \u003Ccode>true != 0\u003C/code>, so the result is \u003Ccode>false\u003C/code> and \u003Ccode>f()\u003C/code> is not called. In other words:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">if ((p--) &gt; m == 0) f(p);\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Please don’t waste too much time on such questions. They have been popular for befuddling novices since before C++ was invented.\u003Cbr>\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3>\u003Cstrong>🥉 \u003C/strong>\u003Ca href=\"https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list\">\u003Cstrong>The Definitive C++ Book Guide and List\u003C/strong>\u003C/a>\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:image -->\n\u003Cfigure class=\"wp-block-image\">\u003Cimg src=\"https://lh3.googleusercontent.com/gTFgU1E25fyNETHbxTMIj4vxeRBNqwu-abOoE_GfP6Wk04SIZwIpj_k_WW0VCu1BItUX5J91WbSfeyNWoZwW7x6gzQoqhoaoUiUsSJTmNOubjlaMjD-XpoqOtu-Y_tpYOfblCkEJ\" alt=\"The-Definitive-C---Book-Guide-and-List\"/>\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Unfortunately, there is no definite C++ book list. There can’t be one. Not everyone needs the same information, not everyone has the same background, and C++ best practices are evolving.\u003Cbr>\u003Cbr>I did a search of the web and found a bewildering set of suggestions. Many were seriously outdated and some were bad from the start. A novice looking for a good book without guidance will be very confused!\u003Cbr>\u003Cbr>You do need a book because the techniques that make C++ effective are not easily picked up from a few blogs on specific topics—and of course blogs also suffer from mistakes, being dated, and poor explanations. Often, they also focus on advanced new stuff and ignore the essential fundamentals.\u003Cbr>\u003Cbr>I recommend my \u003Ca href=\"https://amzn.to/2Ob6lcH\">\u003Cem>Programming: Principles and Practice Using C++ (2nd Edition)\u003C/em>\u003C/a> for people just beginning to learn to program, and \u003Ca href=\"https://amzn.to/2LGYthA\">\u003Cem>A Tour of C++ (2nd Edition)\u003C/em>\u003C/a> for people who are already programmers and need to know about modern C++. People with a strong mathematical background can start with Peter Gottschling‘s \u003Ca href=\"https://amzn.to/2LEFCDS\">\u003Cem>Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers\u003C/em>\u003C/a>.\u003Cbr>\u003Cbr>Once you start using C++ for real, you need a set of guidelines to distinguish what can be done and what is good practice. For that, I recommend the \u003Ca href=\"https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md\">\u003Cem>C++ Core Guidelines on GitHub\u003C/em>\u003C/a>.\u003Cbr>\u003Cbr>For good brief explanations of individual language features and standard-library functions, I recommend \u003Ca href=\"http://www.cppreference.com/\">www.cppreference.com\u003C/a>.\u003Cbr>\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3>\u003Cstrong>#4. \u003C/strong>\u003Ca href=\"https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in\">\u003Cstrong>What are the differences between a pointer variable and a reference variable in C++?\u003C/strong>\u003C/a>\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:image -->\n\u003Cfigure class=\"wp-block-image\">\u003Cimg src=\"https://lh3.googleusercontent.com/Th8zN7ETxb8SbzLzC58N7GkqHT_GAhb8_pFhQg1MfqaXJi7UGZZp4fePBrErD3BiPXBKj7EF4_ajWN8qY0g5NxBXLBp_YX-8CviFYlo_1iEwiDm0KOju_5HAULBBgHHzM3Am5gv1\" alt=\"What-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in-C---\"/>\u003Cfigcaption>To learn more about references and pointers, check out \u003Ca href=\"http://www.codecademy.com/learn/learn-c-plus-plus\">Learn C++\u003C/a>. 😊\u003C/figcaption>\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Both are represented in memory as a machine address. The difference is in their use.\u003Cbr>\u003Cbr>To initialize a pointer, you give it the address of an object:\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">int x = 7;\u003Cbr>int* p1 = &amp;x;\u003Cbr>int* p2 = new int{9};\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>To read and write through a pointer, we use the dereference operator (\u003Ccode>*\u003C/code>):\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">*p1 = 9;&nbsp; &nbsp; &nbsp;  // write through p1\nint y = *p2;&nbsp;  // read through p2\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>When we assign one pointer to another, they will both point to the same object:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">p1 = p2;&nbsp; &nbsp; &nbsp;  // now p1 p2 both point to the int with the value 9\n*p2 = 99; &nbsp; &nbsp;  // write 99 through p2\nint z = *p1;&nbsp;  // reading through p1, z becomes 99 (not 9)\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Note that a pointer can point to different objects during its lifetime. That’s a major difference from references. A reference is bound to an object when it is created and cannot be made to refer to another.\u003Cbr>\u003Cbr>For references, dereferencing is implicit. You initialize a reference with an object and the reference takes its address.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">int x = 7;\u003Cbr>int&amp; r1 = x;\u003Cbr>int&amp; r2 = *new int{9};\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Operator new returns a pointer, so I had to dereference it before assigning using it to initialize the reference.\u003Cbr>\u003Cbr>To read and write through a reference, we just use the reference’s name (no explicit dereferencing):\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">r1 = 9;&nbsp; &nbsp; &nbsp; &nbsp; // write through r1\u003Cbr>int y = r2;&nbsp; &nbsp; // read through r2\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>When we assign one reference to another, the value referred to will be copied:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">r1 = r2; &nbsp; &nbsp; &nbsp; // now p1 and p2 both have the value 9\u003Cbr>r1 = 99; &nbsp; &nbsp; &nbsp; // write 99 through r1\u003Cbr>int z = r2;&nbsp; &nbsp; // reading through r2, z becomes 9 (not 99)\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Both references and pointers are frequently used as function arguments:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">void f(int* p)\n\n{\n&nbsp;&nbsp;&nbsp;&nbsp;if (p == nullptr) return;\n&nbsp;&nbsp;&nbsp;&nbsp;// ...\n}\n\nvoid g(int&amp; r)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;// ...\n}\n\nint x = 7;\nf(&amp;x);\ng(x);\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>A pointer can be the \u003Ccode>nullptr\u003C/code>, so we have to consider whether it points to anything. A reference can be assumed to refer to something.\u003Cbr>\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3>\u003Cstrong>#5. \u003C/strong>\u003Ca href=\"https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string\">\u003Cstrong>How do I iterate over the words of a string?\u003C/strong>\u003C/a>\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:image -->\n\u003Cfigure class=\"wp-block-image\">\u003Cimg src=\"https://lh5.googleusercontent.com/f2i5D68DlWakuxx7M7PVm4tZYvndQsVByLrE-jaL0l03ODV_fkdmsEqpbQD_y9zqTKs2xMDNht6brueJG0o2XWWeb90Lc2lgCEEBZH_clAzE4MW5wJd6DstRFaq3e33F_DNUU3bJ\" alt=\"How-do-I-iterate-over-the-words-of-a-string-\"/>\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Use a \u003Ccode>stringstream\u003C/code>, but how do you define “a word”? Consider \u003Cstrong>“Mary had a little lamb.”\u003C/strong> Is the last word \u003Cstrong>“lamb”\u003C/strong> or \u003Cstrong>“lamb.”\u003C/strong>? If there is no punctuation, it is easy:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">vector&lt;string&gt; split(const string&amp; s)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;stringstream ss(s);\n&nbsp;&nbsp;&nbsp;&nbsp;vector&lt;string&gt; words;\n&nbsp;&nbsp;&nbsp;&nbsp;for (string w; ss&gt;&gt;w; ) words.push_back(w);\n&nbsp;&nbsp;&nbsp;&nbsp;return words;\n}\n\nauto words = split(\"here is a simple example\");&nbsp;  // five words\nfor (auto&amp; w : words) cout &lt;&lt; w &lt;&lt; '\\n';\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>or just:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">for (auto&amp; w : split(\"here is a simple example\")) cout &lt;&lt; w &lt;&lt; '\\n';\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>By default, the \u003Ccode>&gt;&gt;\u003C/code> operator skips whitespace. If we want arbitrary sets of delimiters, things get a bit more messy:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">template&lt;typename Delim&gt;\nstring get_word(istream&amp; ss, Delim d)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;string word;\n&nbsp;&nbsp;&nbsp;&nbsp;for (char ch; ss.get(ch); )&nbsp;   // skip delimiters\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!d(ch)) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word.push_back(ch);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;for (char ch; ss.get(ch); )&nbsp;   // collect word\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!d(ch))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word.push_back(ch);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;\n&nbsp;&nbsp;&nbsp;&nbsp;return word;\n}\n\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>The \u003Ccode>d\u003C/code> is an operation telling whether a character is a delimiter and I return \u003Ccode>\"\"\u003C/code> (the empty string) to indicate that there wasn’t a word to return.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:preformatted -->\n\u003Cpre class=\"wp-block-preformatted\">vector&lt;string&gt; split(const string&amp; s, const string&amp; delim)\u003Cbr>{\u003Cbr>&nbsp;&nbsp;&nbsp;&nbsp;stringstream ss(s);\u003Cbr>&nbsp;&nbsp;&nbsp;&nbsp;auto del = [&amp;](char ch) { for (auto x : delim) if (x == ch) return true; return false; };\u003Cbr>\u003Cbr>&nbsp;&nbsp;&nbsp;&nbsp;vector&lt;string&gt; words;\u003Cbr>&nbsp;&nbsp;&nbsp;&nbsp;for (string w; (w = get_word(ss, del))!= \"\"; ) words.push_back(w);\u003Cbr>&nbsp;&nbsp;&nbsp;&nbsp;return words;\u003Cbr>}\u003Cbr>\u003Cbr>auto words = split(\"Now! Here is something different; or is it? \", \"!.,;? \");\u003Cbr>for (auto&amp; w : words) cout &lt;&lt; w &lt;&lt; '\\n';\u003Cbr>\u003C/pre>\n\u003C!-- /wp:preformatted -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>If you have the C++20 Range library, you don’t have to write something like this yourself but can use a \u003Ccode>split_view\u003C/code>.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:separator -->\n\u003Chr class=\"wp-block-separator\"/>\n\u003C!-- /wp:separator -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>\u003Cem>Bjarne Stroustrup is a Technical Fellow and Managing Director at Morgan Stanley in New York City and a Visiting Professor at Columbia University. He's also the creator of C++.\u003C/em>\u003Cbr>\u003Cbr>\u003Cem>For more information on C++20: \u003C/em>\u003Ca href=\"https://isocpp.org/\">\u003Cem>https://isocpp.org\u003C/em>\u003C/a>\u003Cem>.\u003C/em>\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>\u003Cbr>\u003Cbr>Done reading this awesome post? We have something fun for ya. The Stack Overflow podcast is back! Come \u003Ca href=\"https://stackoverflow.blog/2019/10/15/the-stack-overflow-podcast-is-back/\">check it out\u003C/a> or listen below.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:html -->\n\u003Cfigure>\u003Ciframe height=\"200px\" width=\"100%\" src=\"https://player.simplecast.com/0f1d2d84-566f-4a44-a07c-d619e7ac0d01?dark=false\">\u003C/iframe>\u003C/figure>\n\u003C!-- /wp:html -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>\u003Cbr>\u003C/p>\n\u003C!-- /wp:paragraph -->","html","2019-10-11T18:29:50.000Z",{"current":760},"c-creator-bjarne-stroustrup-answers-our-top-five-c-questions",[762,770,772,777,781,786,791],{"_createdAt":763,"_id":764,"_rev":765,"_type":766,"_updatedAt":763,"slug":767,"title":769},"2023-05-23T16:43:21Z","wp-tagcat-bulletin","9HpbCsT2tq0xwozQfkc4ih","blogTag",{"current":768},"bulletin","Bulletin",{"_createdAt":763,"_id":764,"_rev":765,"_type":766,"_updatedAt":763,"slug":771,"title":769},{"current":768},{"_createdAt":763,"_id":773,"_rev":765,"_type":766,"_updatedAt":763,"slug":774,"title":776},"wp-tagcat-code-for-a-living",{"current":775},"code-for-a-living","Code for a Living",{"_createdAt":763,"_id":778,"_rev":765,"_type":766,"_updatedAt":763,"slug":779,"title":780},"wp-tagcat-codeacademy",{"current":780},"codeacademy",{"_createdAt":763,"_id":782,"_rev":765,"_type":766,"_updatedAt":763,"slug":783,"title":785},"wp-tagcat-engineering",{"current":784},"engineering","Engineering",{"_createdAt":763,"_id":787,"_rev":765,"_type":766,"_updatedAt":763,"slug":788,"title":790},"wp-tagcat-stackoverflow",{"current":789},"stackoverflow","Stackoverflow",{"_createdAt":763,"_id":787,"_rev":765,"_type":766,"_updatedAt":763,"slug":792,"title":790},{"current":789},"C++ Creator Bjarne Stroustrup Answers Our Top Five C++ Questions",[795,801,807,813],{"_id":796,"publishedAt":797,"slug":798,"sponsored":12,"title":800},"28e560af-f0aa-4d46-bd90-f435ad604aa7","2026-06-26T14:00:27.102Z",{"_type":10,"current":799},"paging-charity-how-can-engineering-leaders-avoid-becoming-bond-villains","Paging Charity! How can engineering leaders avoid becoming Bond villains?",{"_id":802,"publishedAt":803,"slug":804,"sponsored":12,"title":806},"4b22c2a3-3779-4966-93eb-5230391dbdce","2026-06-23T14:08:58.595Z",{"_type":10,"current":805},"your-ai-shipped-a-backend-that-boots-that-is-the-whole-problem","Your AI shipped a backend that boots. That is the whole problem.",{"_id":808,"publishedAt":809,"slug":810,"sponsored":12,"title":812},"5cf362e1-fe7b-45af-b69c-914731c6a052","2026-06-23T14:00:00.000Z",{"_type":10,"current":811},"the-2026-developer-survey-is-now-open-for-human-developers-only","The 2026 Developer Survey is now open (for human developers only)!",{"_id":814,"publishedAt":815,"slug":816,"sponsored":12,"title":818},"30b995f7-7cb9-4dd8-bf71-d0685940a32b","2026-06-19T14:00:00.000Z",{"_type":10,"current":817},"dispatches-from-o-reilly-from-capabilities-to-responsibilities","Dispatches from O'Reilly: From capabilities to responsibilities",{"data":820,"sourceMap":-1},{"count":821,"lastTimestamp":822},8,"2023-10-23T12:38:19Z"]