Basic sorttok() and functions

By Sonny on Apr 25, 2010

Just a basic example of a sorttok() function using the STL algorithm sort() and vectors.. It sorts in the order of symbol, number, letter. Just an example, I don't plan to add onto it.

Note: The gettok() function is from NIGathan's str_tok.h snippet. I also have a gettok() function, but it's a very basic example, and doesn't support negative numbers, NIG's is much better. The addtok and numtok are basic examples as well. All of them are useful in a very basic fashion.

Also, included at the very top is another very simple function to convert integers to strings, using stringstream. It's only ever needed when dealing with certain functions where you want to return an integer as a string.

Although I don not plan on adding onto this, feel free to post bugs and in my free time I'll fix what i can.

Link to NIG's snippet:

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

string itostr(int a)
    stringstream b;
    b >> a;
    return b.str();

int numtok(string a, string delim)
    if (a.size() == 0) return 0;
    int tok = 1;
    for (int i = 0;i<a.size();i++)
        if (a.substr(i,1) == delim) tok++;
    return tok;

string addtok(string token, string subtok, string delim)
    return token.insert(token.size(),delim + subtok);

string gettok(string tokens, int tok, string delim)
     int x = 1, y = 0;
     string ret;
     if (tok < 0) tok = numtok(tokens,delim)+tok+1;
     for (int a = 0; a < tokens.size(); a++)
         if (,1,delim) == 0) x++;
         if (x == tok) y = x;
         if (y > 0)
                  if (x > y) break;
     if (,1,delim) == 0) return ret.erase(0,1);
     else return ret;
string sorttok(string token, string delim)
    vector<string> tokens;
    vector<string>::const_iterator iter;
    int t = numtok(token,delim);
    for (int i = 1;i<=t;i++)
    for (iter = tokens.begin();iter != tokens.end();++iter)
        token = addtok(token,*iter, delim);
    return token;


Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.