-3

我有一个字符串,第一个元素是例如'a'。我已经声明了一个名为 a 的变量(例如 int a=1)。我现在的问题是,如何将整个字符串转换为数字(a=1,b=2,c=3,...z=26)?例子:

string str="hello";这必须更改为"85121215"然后更改为 85121215。

4

4 回答 4

3
// transformation itself doesn't care what encoding we use
std::string transform_string(std::string const &in, std::function<int(char)> op)
{
    std::ostringstream out;
    std::transform(in.begin(), in.end(),
                   std::ostream_iterator<int>(out),
                   op);
    return out.str();
}

// the per-character mapping is easy to isolate
int ascii_az_map(char ch)
{
    if (ch < 'a' || ch > 'z') {
        std::ostringstream error;
        error << "character '" << ch << "'=" << (int)ch
              << " not in range a-z";
        throw std::out_of_range(error.str());
    }
    return 1 + ch - 'a';
}

// so we can support other encodings if necessary
// NB. ebdic_to_ascii isn't actually implemented here
int ebcdic_az_map(char ch)
{
    return ascii_az_map(ebcdic_to_ascii(ch));
}

// and even detect the platform encoding automatically (w/ thanks to Phresnel)
// (you can still explicitly select a non-native encoding if you want)
int default_az_map(char ch)
{
    #if ('b'-'a' == 1) && ('j' - 'i' == 1)
        return ascii_az_map(ch);
    #elif ('j'-'i' == 8)
        return ebcdic_az_map(ch);
    #else
        #error "unknown character encoding"
    #endif
}

// use as:
std::string str = "hello";
std::string trans = transform_string(str, ascii_az_map);
// OR ... transform_string(str, ebcdic_az_map);

Note that since the per-character mapping is completely isolated, it's really easy to change the mapping to a lookup table, support different encodings etc.

于 2012-07-26T15:10:07.203 回答
1

你的定义有点小:

"hello" = "85121215
h = 8
e = 5
l = 12
o = 15

我假设你的意思是

a = 1
b = 2
...
z = 26

在这种情况下,这并不难:

std::string meh_conv(char c) {
    switch(c) { // (or `switch(tolower(c))` and save some typing)
    case 'a': case 'A': return "1";
    case 'b': case 'B': return "2";
    ....
    case 'z': case 'Z': return "26";
    ....
    // insert other special characters here
    }
    throw std::range_error("meh");
}

std::string meh_conv(std::string const &src) {
    std::string dest;
    for (const auto c : s)
       dest += meh_conv(c);
    return dest;
}

或使用std::transform()

#include <algorithm>

std::string dest;
std::transform (src.begin(), src.end(), back_inserter(dest),
                meh_conv)

(不适用于不同的传入和传出类型,至少不是这样)


附录。

您可能想要参数化替换地图:

std::map<char, std::string> repl;
repl['a'] = repl['A'] = "0";
repl[' '] = " ";


std::string src = "hello";
std::string dest;
for (const auto c : src) dest += repl[c];
于 2012-07-26T14:46:28.210 回答
0

我给你写了一个简单的例子。它创建一个包含 a-1、b-2、c-3 ... 对的地图。然后使用字符串流连接这些值:

#include <iostream>
#include <map>
#include <sstream>

int main()
{
    std::string str = "abc";
    std::map<char,int> dictionary;
    int n = 1;
    for(char c='a'; c<='z'; c++)
        dictionary.insert(std::pair<char,int>(c,n++));

    //EDIT if you want uppercase characters too:
    n=1;
    for(char c='A'; c<='Z'; c++)
        dictionary.insert(std::pair<char,int>(c,n++));        

    std::stringstream strstream;

    for(int i=0; i<str.size(); i++)
        strstream<<dictionary[str[i]];

    std::string numbers = strstream.str();

    std::cout<<numbers;

    return 0;
}

C++ 专家可能会因为这个解决方案而杀了我,但它确实有效;)

于 2012-07-26T14:39:52.533 回答
0

简单的方法,你可以找到 96 的 char 的 mod(a 之前的 ASCII 值),结果它总是会给你 1-26 范围内的值。

int value;
string s;
cin>>s;
for(int i=0; i<s.size();i++){
    value = s[j]%96;
    cout<<value<<endl;

}
于 2017-01-28T14:13:48.003 回答