如何将字符串转换为大写。我从谷歌上找到的例子只涉及字符。


当前回答

如果您只关心8位字符(除了Milan babukov以外的所有答案都假设),您可以通过在编译时使用元编程生成查找表来获得最快的速度。在ideone.com上,这比库函数快7倍,比手写版本快3倍(http://ideone.com/sb1Rup)。它也可以通过特征自定义,没有减速。

template<int ...Is>
struct IntVector{
using Type = IntVector<Is...>;
};

template<typename T_Vector, int I_New>
struct PushFront;
template<int ...Is, int I_New>
struct PushFront<IntVector<Is...>,I_New> : IntVector<I_New,Is...>{};

template<int I_Size, typename T_Vector = IntVector<>>
struct Iota : Iota< I_Size-1, typename PushFront<T_Vector,I_Size-1>::Type> {};
template<typename T_Vector>
struct Iota<0,T_Vector> : T_Vector{};

template<char C_In>
struct ToUpperTraits {
    enum { value = (C_In >= 'a' && C_In <='z') ? C_In - ('a'-'A'):C_In };
};

template<typename T>
struct TableToUpper;
template<int ...Is>
struct TableToUpper<IntVector<Is...>>{
    static char at(const char in){
        static const char table[] = {ToUpperTraits<Is>::value...};
        return table[in];
    }
};

int tableToUpper(const char c){
    using Table = TableToUpper<typename Iota<256>::Type>;
    return Table::at(c);
}

用例:

std::transform(in.begin(),in.end(),out.begin(),tableToUpper);

为了深入(多页)描述它是如何工作的,请允许我无耻地插入我的博客:http://metaporky.blogspot.de/2014/07/part-4-generating-look-up-tables-at.html

其他回答

本页上的所有这些解决方案都比它们需要的要难。

这样做

RegName = "SomE StRing That you wAnt ConvErTed";
NameLength = RegName.Size();
for (int forLoop = 0; forLoop < NameLength; ++forLoop)
{
     RegName[forLoop] = tolower(RegName[forLoop]);
}

RegName是你的字符串。 获取字符串大小不要使用string.size()作为实际的测试器,非常混乱 可能会引起问题。 然后。最基本的for循环。

记住,字符串大小也返回分隔符,所以在循环测试中使用<而不是<=。

输出将是: 你想转换的字符串

@dirkgently的回答非常鼓舞人心,但我想强调的是,出于如下所示的担忧,

与from的所有其他函数一样,如果实参的值既不能表示为unsigned char也不等于EOF,则std::toupper的行为是未定义的。为了安全地将这些函数与普通字符(或有符号字符)一起使用,参数首先应该转换为无符号字符 参考:std:: toupper

由于标准没有指定plain char是带符号的还是无符号的[1], std::toupper的正确用法应该是:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <string>

void ToUpper(std::string& input)
{
    std::for_each(std::begin(input), std::end(input), [](char& c) {
        c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
    });
}

int main()
{
    std::string s{ "Hello world!" };
    std::cout << s << std::endl;
    ::ToUpper(s);
    std::cout << s << std::endl;

    return 0;
}

输出:

Hello world!
HELLO WORLD!
std::string str = "STriNg oF mIxID CasE lETteRS"

C + 11 +

使用for_each std:: for_each (str.begin (), str.end (), [] (char & c) {c =: toupper (c);}); 使用转换 std::变换(str.begin (), str.end (), str.begin ():: toupper);

c++(仅限windows)

_strupr_s(str, str.length());

c++(使用Boost库)

boost::to_upper_copy(str)
std::string value;
for (std::string::iterator p = value.begin(); value.end() != p; ++p)
    *p = toupper(*p);

我的解决方案(清除第6位):

#include <ctype.h>

inline void toupper(char* str)
{
    while (str[i]) {
        if (islower(str[i]))
            str[i] &= ~32; // Clear bit 6 as it is what differs (32) between Upper and Lowercases
        i++;
    }
}