Read from console in C++

Wed 01 January 2020
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main() {
  // read 2 integers, seperated by space
  int a, b;
  cout << "Enter 2 integers, seperated by space: ";
  cin >> a >> b;
  cout << "a: " << a << ", b: " << b << endl;

  // read single word c-style string.
  // NB: May not contain spaces.
  char season …

Category: Snippets [C++]

Read More

Split a string in C++

Wed 01 January 2020
#include <string>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

vector<string> split(const string& s, char delimiter) {
   vector<string> tokens;
   string token;
   istringstream tokenStream(s);
   while (getline(tokenStream, token, delimiter)) {
      tokens.push_back(token);
   }
   return tokens;
}

int main() {
  string a = "";
  string b = "ab^cd";
  string c = "ab^cd …

Category: Snippets [C++]

Read More
Page 1 of 1