标签: stdstring

字符串包含有效字符

我正在写一个签名的方法

bool isValidString(std::string value)
Run Code Online (Sandbox Code Playgroud)

在这个方法里面我想搜索value属于一组字符的所有字符,这是一个常量字符串

const std::string ValidCharacters("abcd")
Run Code Online (Sandbox Code Playgroud)

要执行此搜索,我从中value搜索并搜索一个字符 ValidCharacters,如果此检查失败,那么它是无效的字符串是否在STL库中有任何其他替代方法来执行此检查.

c++ stl stdstring

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

编译器通常对字符串有特殊的优化吗?

很多时候你会看到类似的东西

std::map<std::string, somethingelse> m_named_objects;
Run Code Online (Sandbox Code Playgroud)

要么

std::string state;

//...

if(state == "EXIT")
   exit();
else if(state == "california")
   hot();
Run Code Online (Sandbox Code Playgroud)

人们使用字符串纯粹是为了让事物更具可读性.使用整数ID可以轻松实现同样的功能.

现代编译器(msvc,g ++等)通常可以对这些类型的案例采用特殊优化吗?或者是否应该因性能不佳或其他原因而避免这种情况?

c++ string stdstring compiler-optimization

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

将标准C++字符串转换为String ^

我想在Visual C++环境中将std :: string转换为System :: String ^.我知道我们可以通过MarshalStringFunction 将System :: String转换为std :: string,如下所示:

void MarshalString ( String ^ s, string& os ) {
    using namespace Runtime::InteropServices;
    const char* chars = 
        (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
    os = chars;
    Marshal::FreeHGlobal(IntPtr((void*)chars));
}
Run Code Online (Sandbox Code Playgroud)

我找不到将std :: string转换为System :: String的方法,但我发现System :: String的构造函数带有如下参数:

System::String(Char* value, Int32 startIndex, Int32 length)
Run Code Online (Sandbox Code Playgroud)

我尝试使用下面的代码,但它无法给我一个正确的解决方案:

std::string str1 = "MyString";
System::String^ str = new System::String(str1.c_str(), 0, str1.length());
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

string c++-cli stdstring visual-c++

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

是否可以模板basic_string <> :: iterator?

可能重复:
我必须在何处以及为何要使用"template"和"typename"关键字?

我使用g ++ 4.6,我试图根据使用的char类型模拟我的类,但我的变量不仅仅是字符,而是字符串的字符串.所以我尝试过这样的事情:

template<typename T>
class Node
//... 
//constructor
Node(std::basic_string<T> str, std::basic_string<T>::iterator it)
{
}
//usage 
Node<char16_t> start;
Run Code Online (Sandbox Code Playgroud)

但我得到'std :: basic_string <_CharT,std :: char_traits <_CharT>,std :: allocator <_Tp1 >> :: iterator'不是类型
当我在构造函数arg列表中替换第二个T为char16_t时它编译.

c++ templates stdstring c++11

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

如何将std :: string转换为C风格的字符串

我用C++编程.

这个问题基本上是我无法在任何地方找到答案.所以这是问题所在:

我想创建一个C风格的字符串,但我想将一个整数变量i放入字符串中.很自然地,我使用了一个流:

stringstream foo;
foo
                << "blah blah blah blah... i = " 
                << i
                << " blah blah... ";
Run Code Online (Sandbox Code Playgroud)

但是,我需要以某种方式获得一个C风格的字符串传递给一个函数(原来foo.str()返回一个std :: string).所以这在技术上是一个三部分问题 -

1)如何将std :: string转换为C风格的字符串?

2)有没有办法从字符串流中获取C风格的字符串?

3)有没有办法直接使用C风格的字符串(不使用字符串流)来构造一个带有整数变量的字符串?

c++ string cstring stdstring

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

抛出'std :: out_of_range'的实例后调用terminate():basic_string :: erase

string Farfallino::decode(string buff) {

string stringa;
size_t pos;

while(1) {
    while(pos = (buff.find("afa"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "a");
    }
    while(pos = (buff.find("efe"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "e");
    }
    while(pos = (buff.find("ifi"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "i");
    }
    while(pos = (buff.find("ofo"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "o");
    }
    while(pos = (buff.find("ufu"))) {
        buff.erase(pos, 3);
        buff.insert(pos, "u");
    }
}

return stringa;
}
Run Code Online (Sandbox Code Playgroud)

我试图擦除传递给函数的字符串中的每个"afa""efe""ifi""ofo"和"ufu",但它给了我这个错误.我不知道我做错了什么..

c++ stdstring erase outofrangeexception

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

为我的字符串类实现 reverse_iterator(还有 rbegin() 和 rend() 方法)

下面是我的 String 类的代码。我想实现 reverse_iterator 和 rbegin() 和 rend() 方法。已粘贴分配方法的代码。String::reverse_iterator rbegin = str2.rbegin(); String::reverse_iterator rend = str2.rend(); for(String::reverse_iterator b = rbegin; b!= rend;++b) { cout<<*b; }

class String {//my custom string class

public:

    class iterator:public std::iterator<std::random_access_iterator_tag, char> {
    public:
        iterator():ch(NULL){}
        iterator(const iterator& it) : ch(it.ch) {}

        char& operator*() { return *ch; }
        iterator& operator++() {
            ch = ch+1;
            return *this;
        }
        bool operator==(const iterator& rhs) {
            return ch == rhs.ch;
        }
        bool operator!=(const iterator& rhs) {
            return ch != rhs.ch;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stdstring reverse-iterator

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

使用string的c_str()函数的C++奇怪行为

我正在将我的项目从Visual Studio 06移动到2010.在这样做时,我在我的代码中观察到了这种行为.我有一个Get字符串函数,如下所示:

string GetTheStr() 
{ 
        return strSomeStdString; 
} 
Run Code Online (Sandbox Code Playgroud)

然后有另一个函数调用上面的get函数,如下所示:

const char* ptrStr = (char *)GetTheStr().c_str();
Run Code Online (Sandbox Code Playgroud)

ptrStr指向的字符串的值是""

上面的代码在visual studio 06中运行良好,但在visual studio 2010上却没有.

然后我尝试了几个实验:

std::string str = GetTheStr(); // -> value inside str displayed correctly
const char* PtrCStr = str.c_str(); // -> value pointed by PtrCStr displayed correctly
const char* PtrData = str.data(); // -> value pointed by PtrData displayed correctly
const char* ptr = (char *)GetTheStr().c_str(); // -> value pointed by ptr NOT displayed correctly
Run Code Online (Sandbox Code Playgroud)

我想知道为什么最后一行不起作用.任何人都可以告诉我为什么上面的行为发生在Visual Studio 2010而不是视觉工作室06?

提前致谢 :)

c++ stdstring

1
推荐指数
2
解决办法
926
查看次数

C++传递std :: string的最快方法?

注意我问这是一个std::string特定的问题,而不是一般如何传递一个对象.

我会删除这个问题,但由于它的答案,我不被允许.我相信答案可能倾向于堕落,以回答更常见的问题:"用C++传递对象的最佳方法是什么?" 关于堆栈溢出的这些更一般的答案有许多重复,即使这个精确的问题本身可能不是重复.这种退化可能是因为问题是一个不合适的问题,而且std :: string类没有什么特别之处.这个问题并没有高度投票,这表明它并不有趣.在那种情况下我很糟糕.也许一个mod会看到这个黑色的文字,怜悯我并杀死这个问题.

以下哪个签名表示将非const std::string实例传递给不修改它的函数的最快方法,考虑到调用站点上调用的总体开销,包括底层字符数组的深层副本是否会在通话之前产生?

extern void eat_string_by_value          (std::string s);
extern void eat_const_string_by_value    (const std::string s);
extern void eat_string_by_reference      (std::string& s);
extern void eat_const_string_by_reference(const std::string& s);
Run Code Online (Sandbox Code Playgroud)

注意我问这是一个std::string特定的问题,而不是一般如何传递一个对象.特别是,我的问题是由以下因素刺激:

  • std :: string handle-body implementation:是否有任何签名暗示了在调用站点的字符串主体及其后备字符数组的深层副本?
    • 注意复制句柄(应该是轻量级)和复制主体(这将涉及内存分配)之间的区别.
  • C++ 98与C++> = 11:这个版本划分的任何一方是否有不同的答案?

我没有看到我的问题是前一个问题的副本,它本身标记为一个普通对象的副本,传递一个< 传递参数为std :: string或const std :: string&?>因为这些特定类型的详细点.

关于相关问题的一个有趣答案:

c++ deep-copy stdstring c++11 c++98

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

malloc和calloc与std :: string之间的区别

我最近进入了C++,我遇到了使用malloc的问题.下面的代码不会打印出"成功"(程序崩溃,退出代码为0xC0000005),而如果我使用calloc,则一切正常.

int main(){
    std::string* pointer = (std::string*) malloc(4 * sizeof(std::string));

    for(int i = 0; i < 4; i++){
        pointer[i] = "test";
    }

    std::cout << "Success" << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下代码有效.

calloc(4, sizeof(std::string));
Run Code Online (Sandbox Code Playgroud)

如果我分配正常数量的12倍,Malloc也可以工作.

有人可以解释这种行为吗?这与std :: string有关吗?

c++ malloc stdstring calloc

1
推荐指数
2
解决办法
342
查看次数