mix std::getline() with cin>>

Useful in coding tests

#include <limits>
using namespace std;

int main(){ //mixing cin>> and getline()
  int N;
  cout<<"enter N:"<<endl; cin>>N;
  cout<<"N = "<<N<<endl;
  cin >> std::ws; //special manipulator1
  string s;
  cout<<"Now enter a line with optional spaces"<<endl;
  cout<<"s = "<<s<<endl;

custom delimiter for cin operator>> #complicated

Tested but is too hard to remember. Better use the getline() trick in https://bintanvictor.wordpress.com/2017/11/05/simplest-cway-to-split-string-on-custom-delimiter/

struct comma_is_space : std::ctype<char> { //use comma as delimiter
  comma_is_space() : std::ctype<char>(get_table()) {}
  static mask const* get_table() {
    static mask rc[table_size];
    rc[','] = std::ctype_base::space;
    return &rc[0];

istringstream iss(line);
iss.imbue(locale(cin.getloc(), new comma_is_space));

the "stream" concept in c++/c#/java

C had ascii/binary read/write functions. C++ introduced a family of “stream” classes as a complete replacement.

java and c# embraced and expanded the stream concepts and kissed goodby to the C legacy. I guess stream must be an immensely successful invention. Before we look at all the new stream classes like MemoryStream, GzipStream, StringStream… it’s good to understand the fundamental innovation. See http://www.cprogramming.com/tutorial/c++-iostreams.html.

I feel streams have become more versatile over the decades. Initially IO constructs for file and socket only. Then IPC. Then serialization. Then …

I particularly like the ostringstream — the “string builder” in c++.

Can we say the producer-consumer pattern is also stream-based? Probably not. Streams usually hold bytes or characters, not commands/tasks.

c++ stream — IKM findings

——tellg and tellp?
http://answers.yahoo.com/question/index?qid=20110507033952AAXZBz2 is a short explanation.

tellg(void) and tellp(void) both return their pointer’s position
ostream& endl (ostream& os);
stream::rdbuf() changes the filebuf… http://www.cplusplus.com/reference/ios/ios/rdbuf/
endl – flush() implicitly
If your variable to “populate” is an int, then extraction operator stops when hitting ….”any character that couldn’t be part of an int” which is not limited to white space.
—-which stream classes can be used for writing to a file
——which ios modes are used for reading a file

java getResourceAsStream() – easist(@@) way to read any file

You can easily read a file in the same dir (esp. in a jar) as your class or anywhere(?) on file system. Methods below are usually conveniently called on the Class object, like

1) simplest usage:
InputStream is = this.getClass().getResourceAsStream(“any file such as a.txt”);
InputStream is = AnyClass.class.getResourceAsStream(“any file such as a.txt”);

InputStream is = GetResourceTest.class.getResourceAsStream(“../../test.txt”);
if (is==null) System.err.println(“file not found”);

Behind the scene, these methods delegate to the classloader’s getResourceAsStream()
http://mindprod.com/jgloss/getresourceasstream.html has good examples.

ifstream /with-f/ and istream — typedef specializations revealed

Background — “cin” is an instance of istream (no “f”); whereas ifstream is the most common file input utility.

“istream is a specialization (not a template instantiation) of the class template basic_istream” — many people say that but if the generic template is named “basic_istream” a specialization can’t have a different name! When you look at the code you realize it’s yet another typedef —

typedef basic_istream istream

Therefore, istream is an alias to a specialized basic_istream.

Now back to basics. Stripping layers of typedef and template specialization, the core structure is

———> basic_ifstream extends basic_istream extends ….

-) ifstream is a typedef of a specialization of basic_ifstream
-) istream is a typedef of a specialization of basic_istream

typedef basic_istream istream;
typedef basic_ifstream ifstream;

c++ stream format flags and manipulators – key words

bitVector — probably there’s a hidden bitArray of boolean flags for each stream instance. http://www.cplusplus.com/reference/iostream/ios_base/flags/

1-to-1 — one flag for one manipulator function

Concrete eg — http://www.cplusplus.com/reference/iostream/manipulators/showpos/

transformer — a typical manipulator is a function accepting a stream by reference, and returning the same stream object by reference