#include"csv2/reader.hpp"#include<string>usingnamespacestd;intmain(){csv2::Reader<csv2::delimiter<','>,csv2::quote_character<'"'>,csv2::first_row_is_header<true>,csv2::trim_policy::trim_whitespace>csv;std::stringcontent="Name, Age\nPeter, 12\nLucy, 78";if(csv.parse(content)){constautoheader=csv.header();for(constautorow:csv){for(constautocell:row){// Do something with cell value
std::stringvalue;cell.read_value(value);cout<<value<<" ";}cout<<"\n";}}}
#include"csv2/reader.hpp"#include<string>usingnamespacestd;intmain(){csv2::Reader<csv2::delimiter<','>,csv2::quote_character<'"'>,csv2::first_row_is_header<true>,csv2::trim_policy::trim_whitespace>csv;if(csv.mmap("demo.csv")){constautoheader=csv.header();for(constautorow:csv){for(constautocell:row){// Do something with cell value
std::stringvalue;cell.read_value(value);cout<<value<<" ";}cout<<"\n";}}}
classCellIterator{friendclassRow;constchar*buffer_;size_tbuffer_size_;size_tstart_;size_tcurrent_;size_tend_;public:CellIterator(constchar*buffer,size_tbuffer_size,size_tstart,size_tend):buffer_(buffer),buffer_size_(buffer_size),start_(start),current_(start_),end_(end){}CellIterator&operator++(){current_+=1;return*this;}Celloperator*(){boolescaped{false};classCellcell;cell.buffer_=buffer_;cell.start_=current_;cell.end_=end_;size_tlast_quote_location=0;boolquote_opened=false;for(autoi=current_;i<end_;i++){current_=i;if(buffer_[i]==delimiter::value&&!quote_opened){// actual delimiter
// end of cell
cell.end_=current_;cell.escaped_=escaped;returncell;}else{if(buffer_[i]==quote_character::value){if(!quote_opened){// first quote for this cell
quote_opened=true;last_quote_location=i;}else{escaped=(last_quote_location==i-1);last_quote_location+=(i-last_quote_location)*size_t(!escaped);quote_opened=escaped||(buffer_[i+1]!=delimiter::value);}}}}cell.end_=current_+1;returncell;}booloperator!=(constCellIterator&rhs){returncurrent_!=rhs.current_;}};
#include<iostream>usingnamespacestd;// forward-declaration to allow use in Iter
classIntVector;classIter{public:Iter(constIntVector*p_vec,intpos):_pos(pos),_p_vec(p_vec){}// these three methods form the basis of an iterator for use with
// a range-based for loop
booloperator!=(constIter&other)const{return_pos!=other._pos;}// this method must be defined after the definition of IntVector
// since it needs to use it
intoperator*()const;constIter&operator++(){++_pos;// although not strictly necessary for a range-based for loop
// following the normal convention of returning a value from
// operator++ is a good idea.
return*this;}private:int_pos;constIntVector*_p_vec;};classIntVector{public:IntVector(){}intget(intcol)const{return_data[col];}Iterbegin()const{returnIter(this,0);}Iterend()const{returnIter(this,100);}voidset(intindex,intval){_data[index]=val;}private:int_data[100];};intIter::operator*()const{return_p_vec->get(_pos);}// sample usage of the range-based for loop on IntVector
intmain(){IntVectorv;for(inti=0;i<100;i++){v.set(i,i);}for(inti:v){cout<<i<<endl;}}
/**
* Determines the operating system's page allocation granularity.
*
* On the first call to this function, it invokes the operating system specific syscall
* to determine the page size, caches the value, and returns it. Any subsequent call to
* this function serves the cached value, so no further syscalls are made.
*/inlinesize_tpage_size(){staticconstsize_tpage_size=[]{#ifdef _WIN32
SYSTEM_INFOSystemInfo;GetSystemInfo(&SystemInfo);returnSystemInfo.dwAllocationGranularity;#else
returnsysconf(_SC_PAGE_SIZE);#endif
}();returnpage_size;}
#include<utility>// std::forward
#include<iostream>// std::cout
// function with lvalue and rvalue reference overloads:
voidoverloaded(constint&x){std::cout<<"[lvalue]";}voidoverloaded(int&&x){std::cout<<"[rvalue]";}// function template taking rvalue reference to deduced type:
template<classT>voidfn(T&&x){overloaded(x);// always an lvalue
overloaded(std::forward<T>(x));// rvalue if argument is rvalue
}intmain(){inta;std::cout<<"calling fn with lvalue: ";fn(a);std::cout<<'\n';std::cout<<"calling fn with rvalue: ";fn(0);std::cout<<'\n';return0;}