标签: stdstring

最快捷的词汇方式

使用C++将单词(std :: string)大写的最快方法是什么?

在使用带有-O3标志的g ++ 4.6.3的Debian Linux上,使用此函数boost::to_lower将在AMD Phenom(tm)II X6 1090T处理器(3200 MHz)的单个执行线程中大约24秒内占用81,450,625个字.

void Capitalize( std::string& word )
{
    boost::to_lower( word );
    word[0] = toupper( word[0] );
}
Run Code Online (Sandbox Code Playgroud)

此功能使用std::transform大约10秒内完成相同的操作.我在测试之间清除VM,所以我不认为这种差异是侥幸:

sync && echo 3 > /proc/sys/vm/drop_caches

void Capitalize( std::string& word )
{
    std::transform(word.begin(), word.end(), word.begin(), ::tolower);
    word[0] = toupper( word[0] );
}
Run Code Online (Sandbox Code Playgroud)

有更快的方法吗?我不想为了速度而失去可移植性,但是如果有更快的方法可以在std C++或std C++中使用boost工作,我想尝试一下.

谢谢.

c++ boost stdstring

6
推荐指数
3
解决办法
3242
查看次数

我应该使用operator + =而不是operator +来连接std :: string吗?

有没有理由我经常看到这个结构:

std::string myString = someString + "text" + otherString + "more text";
Run Code Online (Sandbox Code Playgroud)

...而不是这个(我很少看到):

std::string myString;
myString += someString += "text" += otherString += "more text";
Run Code Online (Sandbox Code Playgroud)

阅读std::stringAPI,在我看来,operator+创建了很多临时工(可能是由编译器RVO优化?),而operator+=变体只附加文本.

在某些情况下,operator+变体将是要走的路.但是当你只需要将文本附加到现有的非const字符串时,为什么不使用operator+=呢?有什么理由不去?

-Rein

c++ concatenation append stdstring

6
推荐指数
1
解决办法
228
查看次数

从std :: string,C++获取类型

在面试中我被问到一个问题.

因此我有一个函数void f(std::string),我称之为函数f("int").所以我的函数必须int x在它的体内创建一个局部.有没有办法从中获取类型const char*.我知道boost::mpl::vector确实可以解决这类问题.谁能告诉我这项技术?

c++ algorithm types character-encoding stdstring

6
推荐指数
1
解决办法
1086
查看次数

比较包含负字符的字符串时出现令人惊讶的结果

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;

void main()
{
    string a = "a";
    string b(1, -70); /*constructor, create a string having 1 character that its value is equal to -70*/
    cout << (b > a ? b : a);
}

//output on screen: b was printed, not a (!)
Run Code Online (Sandbox Code Playgroud)

虽然b的值小于a的值,为什么b>a?我该如何纠正这种情况?

c++ string comparison stdstring

5
推荐指数
1
解决办法
770
查看次数

使用比较运算符将 std::string 作为 std::map 中的键

我正在尝试使用 std::string 作为 std::map 中的键,但是,我无法正确 find() 。我的代码有点复杂和大,所以这是一个演示我遇到的问题的小程序。如果有人能告诉我为什么这不起作用,我将不胜感激。

谢谢。

#include <stdio.h>
#include <string>
#include <map>

struct comparer
{
    public:
    bool operator()(const std::string x, const std::string y)
    {
         return x.compare(y)==0;
    }
};

int main(int argc, char *argv[])
{
    std::map<std::string, int, comparer> numbers;
    numbers.insert(std::pair<std::string,int>("One",1));
    numbers.insert(std::pair<std::string,int>("Two",2));
    numbers.insert(std::pair<std::string,int>("Three",3));
    numbers.insert(std::pair<std::string,int>("Four",4));
    numbers.insert(std::pair<std::string,int>("Five",5));


    std::map<std::string, int, comparer>::iterator it=numbers.find("Three");
    if(it!=numbers.end())
        printf("The number is %d\n",(*it).second);
    else
        printf("Error, the number is not found\n");
}
Run Code Online (Sandbox Code Playgroud)

c++ key stdmap std stdstring

5
推荐指数
1
解决办法
3万
查看次数

如何将 C++ 字符串中的单词大写?

我有一个 std::string 并希望第一个字母大写,其余字母小写。

我可以这样做的一种方法是:

const std::string example("eXamPLe");
std::string capitalized = boost::to_lower_copy(example);

capitalized[0] = toupper(capitalized[0]);
Run Code Online (Sandbox Code Playgroud)

这将产生capitalized:

“例子”

但也许有更直接的方法来做到这一点?

c++ boost capitalize stdstring

5
推荐指数
1
解决办法
4762
查看次数

为什么 std::string 的成员 operator= 不是左值引用限定的

我最近了解到成员函数可以是ref-qualified,这使我可以编写

struct S {
    S& operator=(S const&) & // can only be used if the implicit object is an lvalue
    { 
      return *this; 
    }
};

S operator+(S const &, S const &) { 
  return {}; 
}
Run Code Online (Sandbox Code Playgroud)

从而阻止用户做类似的事情

S s{};
s + s = S{}; // error
Run Code Online (Sandbox Code Playgroud)

然而,我看到该std::string成员operator= 并没有这样做。所以下面的代码编译时没有警告

std::string s;
s + s = s;
Run Code Online (Sandbox Code Playgroud)

有理由允许这样做吗?

如果没有,将来是否可以添加引用限定符,或者会以某种方式破坏现有代码吗?

c++ operator-overloading stdstring ref-qualifier

5
推荐指数
1
解决办法
184
查看次数

如何编写从 std::invalid_argument 派生的自定义异常类?

我应该如何编写一个高效的异常类来显示可以通过在运行时修复源代码错误来防止的错误?

这就是我选择的原因std::invalid_argument。

我的异常类(显然不起作用):

class Foo_Exception : public std::invalid_argument
{
private:
    std::string Exception_Msg;
public:
    explicit Foo_Exception( const std::string& what_arg );
    virtual const char* what( ) const throw( );
};

explicit Foo_Exception::Foo_Exception( const std::string& what_arg ) // how should I write this function???
{
    Exception_Msg.reserve( 130000 );

    Exception_Msg = "A string literal to be appended, ";
    Exception_Msg += std::to_string( /* a constexpr int */ );
    Exception_Msg += /* a char from a const unordered_set<char> */;
}

const char* Foo_Exception::what( ) …
Run Code Online (Sandbox Code Playgroud)

c++ exception c-strings stdstring micro-optimization

5
推荐指数
1
解决办法
3196
查看次数

如果我肯定要复制字符串,如何有效地将它传递给函数?C++20

我想知道如何正确处理函数中的字符串输入,如果我知道我将必须在其中创建一个副本以将其放入容器中。

我想这样做:

void foo(const string& s){
    container.push(s);
}
Run Code Online (Sandbox Code Playgroud)

但是如果我传递一个 char* 字符串,我将制作 2 个副本,首先是在调用 foo 时,然后是在将其传递到容器时。

我的第二个想法是使用 string_view,正如人们常说的,如果你可以使用视图,你就应该使用。所以我的函数的第二个版本如下所示:

void foo2(string_view s){
    container.push(string(s));
}
Run Code Online (Sandbox Code Playgroud)

好的,现在无论给我什么类型的字符串,我都会只制作一个副本。但后来我开始思考,为什么我不能接受一个字符串作为函数的参数,像这样简化它:

void foo3(string s){
    container.push(std::move(s));
}
Run Code Online (Sandbox Code Playgroud)

但现在我必须确保我的容器正确利用移动语义,这样它最终不会制作另一个副本!由于此容器对象是模板化类型,因此意味着使用完美转发等,这本身就是大量工作。

目前我不知道该选择哪个选项,所以想请教您一些建议。我还觉得我想得太多了,因为这最终可能不会对性能产生太大影响,但我想做得正确。提前致谢。

c++ std stdstring string-view c++20

5
推荐指数
2
解决办法
313
查看次数

如何将 char* 转换为 std::u8string?

介绍

如果我捕获异常,我想将错误消息(该方法以 C 样式字符串形式返回)转换what()为std::u8string(UTF-8 字符串)。例如:std::u8string(error.what());

问题

如何将 a 转换char*为 a std::u8string?

附加信息

  • 我只捕获标准库、boost 和 eigen 中的异常。
  • 我的应用程序依赖于 Windows,因此该解决方案不需要是可移植的。

c++ c-strings utf-8 stdstring c++20

5
推荐指数
1
解决办法
290
查看次数