Raw C strings (char arrays) should always terminate with a null character, ‘\0’. Fun fact about char arrays: nullptr is inserted at the back of the array. This is a convention for argv
1
2
3
4
5
6
7
8
charstr[]="a string";printf("%s",str);// searches for "\0" to terminatechar*strs[]={"str1","str2",nullptr};for(size_ti=0;strs[i]!=nullptr;++i){// uses nullptr to terminate string array. printf("%s",strs[i]);}
Substring
std::string substr(size_t pos = 0, size_t len = std::string::npos) const;
std::string::npos is optional. If not provided, it will be the full string size.
```cpp
### Best way for concactenate variables of different types - use `stringstream`
```cpp
std::ostringstream oss;
oss << "index are not matching! " << idx << "|" << match.idx_in_this_cloud;
throw std::runtime_error(oss.str());
#include<iostream>
#include<sstream>
#include<vector>usingnamespacestd;intmain(){stringstr="(1,2,3)";stringstreamss(str);// read ss // cout<<ss.str()<<endl;ss<<"4,5";// I now see 4,52,3)??cout<<ss.str()<<endl;ss<<str;stringstreamss2(str.substr(1,str.size()-2));stringnum;chardelim=',';std::vector<int>vec;while(getline(ss2,num,delim)){// getline vec.push_back(stoi(num));// in #include <cstdlib>}return0;}
Initialization: std::stringstream ss(str). Internally, ss has a buffer. This sets the internal buffer
<< is pronounced as the “insertion operator”. Once you call that, the internal buffer starts from 0 position
bool done_line = getline(ss2, num, delim) is the powerful tool that parses a line by delimeters
stoi() is the powerful tool to convert a string into an integer