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;
  getline(cin,s);
  cout<<"s = "<<s<<endl;
}
Advertisements

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
ofstream?
fstream?
ostream?
——which ios modes are used for reading a file

IPC sockets, memory mapped files and sysV shmem

Requirement — Large volume data sharing across jvm and other unix processes.

  1. sockets are well supported in java, c, perl, python but still requires copying lots of data. I think only the IPC socket is relevant, here, not inet sockets.
    • named pipes are less popular than localhost sockets and comparable in speed. On some systems, these two are probably the same thing underneath.
  2. memory mapped files as a RandomAccessFile and MappedByteBuffer? Pure java solution — No JNI needed. I feel not so “popular”. c# and c++ also support it.
  3. sysV shmem? Presumably faster than data copying solutions like pipes, message queues, file-based. But JNI needed.

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.