Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a substring by reference?

I call recursively a function passing as argument a substring which always starts from the beginning of the current string up to a position. If I was using C, I could pass the pointer to the first position of the string and then the necessary length. Nevertheless, I would like to achieve the same result using the class string. Is it possible? If I use const, is the compiler smart enough to make the optimization on its own? Even better, is there a way to check on my own whether the compiler actually makes a copy of the argument or passes a reference?

My question was motivated after having written the following code which passes the tests on problem Alphacode on poj, once someone uses atoi instead of atof.

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>

using namespace std;

map<string, int> cache;

bool valid_character_number(string a) {
    return 0 < stoi(a.substr(a.size() - 2, 2)) && stoi(a.substr(a.size() - 2, 2)) <= 26;
}

bool zero_last_digit(string a) {
    return a[a.size() - 1] == '0';
}
bool zero_before_last_digit(string a) {
    return a[a.size() - 2] == '0';
}

int decodings(string a) {
    if (a.size() == 0)
        return 1;
    if (a.size() == 1) {
        if (zero_last_digit(a))
            return 0;
        else
            return 1;
    }
    if (cache.find(a) != cache.end())
        return cache[a];

    if (zero_last_digit(a) && valid_character_number(a))
        return cache[a] = decodings(a.substr(0, a.size() - 2));
    else if (valid_character_number(a) && !zero_before_last_digit(a))
        return cache[a] = decodings(a.substr(0, a.size() - 1)) + decodings(a.substr(0, a.size() - 2));
    else
        return cache[a] = decodings(a.substr(0, a.size() - 1));
}

int main() {
    string input;
    while (true) {
        cin >> input;
        if (input.size() == 1 && stoi(input) == 0)
            return 0;
        cout << decodings(input) << endl;
    }

    return 0;
}
like image 336
Dimitris Leventeas Avatar asked Mar 19 '13 01:03

Dimitris Leventeas


2 Answers

You cannot use std::string for this purpose, but you can easily make a class of your own that holds a pair of iterators (begin and end) into another string, or a C-style char* and size. With C++11 (since you tagged it), you should even be able to make a User Defined Literal syntax for creating strings of your new type.

like image 121
John Zwinck Avatar answered Oct 06 '22 01:10

John Zwinck


You can use your own wrapper class like this one:

struct RefString
{
    RefString(const std::string & s, int i, int l) : s(s), i(i), l(l) {}

    const char & operator [] (int x) const {
        return s[i+x];
    }

    size_t length() const {
        return l;
    }

    bool operator < (const RefString & s2) const {
        return s.compare(i, l, s2.s, s2.i, s2.l) < 0;
    }

private:
    const std::string & s;
    int i;
    int l;
};

std::ostream & operator << (std::ostream &stream, const RefString & ms) {
    for (int i = 0; i < ms.length(); i++)
        stream << ms[i];
    return stream;
}

And use it like this, for example for creating set of unique substrings:

std::string s = "hello";
std::set<RefString> st;
for (int i = 0; i < s.length(); i++)
for (int j = i; j < s.length(); j++)
    st.insert(RefString(s, i, j-i+1));
like image 38
k06a Avatar answered Oct 05 '22 23:10

k06a