【问题标题】:How can I pass a substring by reference?如何通过引用传递子字符串?
【发布时间】:2013-03-19 01:58:06
【问题描述】:

我递归调用一个函数作为参数传递一个子字符串,该子字符串总是从当前字符串的开头开始直到一个位置。如果我使用 C,我可以将指针传递给字符串的第一个位置,然后传递必要的长度。不过,我想使用string 类来实现相同的结果。是否可以?如果我使用const,编译器是否足够聪明,可以自行进行优化?更好的是,有没有办法自己检查编译器是否真的复制了参数或传递了引用?

一旦有人使用atoi而不是atof,我编写了以下代码,通过了poj上问题Alphacode的测试后,我的问题被激发了。

#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;
}

【问题讨论】:

  • 我看不到您的函数修改参数的任何地方。使用const std::string &amp;

标签: c++ algorithm c++11


【解决方案1】:

您不能为此目的使用std::string,但您可以轻松创建自己的类,将一对迭代器(开始和结束)保存到另一个字符串或C 样式的char* 和大小。使用 C++11(因为您标记了它),您甚至应该能够创建用户定义的文字语法来创建新类型的字符串。

【讨论】:

    【解决方案2】:

    您可以像这样使用自己的包装类:

    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;
    }
    

    并像这样使用它,例如创建 set 的唯一子字符串:

    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));
    

    【讨论】:

    • 为什么索引运算符返回一个 const 引用?为什么字符串引用成员是 const 呢?
    • @renonsz 提供的解决方案允许在没有任何字符串复制的情况下拥有子字符串实例。所以不允许修改 RefString 以保持可能的兄弟 RefStrings 不被修改。
    猜你喜欢
    • 1970-01-01
    • 2014-08-11
    • 2017-07-25
    • 2016-07-05
    • 2013-04-07
    • 2018-09-01
    • 1970-01-01
    • 2010-11-21
    • 2010-12-24
    相关资源
    最近更新 更多