我最近越来越多地使用大括号初始化。尽管我发现在这种情况下圆括号初始化存在差异,但我想知道为什么。
如果我做:
const std::string s(5, '=');
std::cout << s << std::endl;
Run Code Online (Sandbox Code Playgroud)
我得到:
=====
Run Code Online (Sandbox Code Playgroud)
这就是我所期望的。但如果我这样做:
=====
Run Code Online (Sandbox Code Playgroud)
我得到:
=
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
编辑:为了任何看到此内容的人的利益。=第二个输出中的之前有一个不可打印的字符。它只是没有显示在 stackoverflow 上。好像:
我正在开发一个依赖于我提供文件路径的项目(例如.C:\Path.pth).现在,我昨天打电话给我std::string:
std::string path(`"C:\\Path.pth`");
Run Code Online (Sandbox Code Playgroud)
但现在它不起作用.它扔了一个bad_alloc.似乎' \'字符是问题.我甚至尝试使用\x5C它的ascii值,但结果相同.
现在,我的问题是,是否有可能我搞砸了一些#define,一些编译器选项或其他可能导致这种情况的"非代码"?我正在使用VS 2005.
任何帮助将非常感激
PierreBdR
..听起来很可能.或者至少,它必须:P
由于没有人提到某种/ SetStringCharSize:2bit-compiler选项,我认为可以安全地假设我的代码必须搞砸某些东西,并且它不仅仅是一个愚蠢的编译器选项(或类似的)错误. .
在问题中,如果我在我的类中定义一个字符串运算符:
class Literal {
operator string const () {
return toStr ();
};
string toStr () const;
};
Run Code Online (Sandbox Code Playgroud)
然后我用它:
Literal l1 ("fa-2bd2bc3e0");
cout << (string)l1 << " Declared" << endl;
Run Code Online (Sandbox Code Playgroud)
使用显式转换一切正常,但如果我删除(字符串)编译器说它需要在std :: string中声明的强制转换运算符.它不应该自动投射我的类型?解决:我正在重载运算符<<(ostream&os,const Literal&l).
谁能告诉我C/C++中c_str()函数的用途是什么?在哪种情况下有必要使用它?
我感兴趣(出于各种原因)格式使用sprintf将结果放在一个std::string对象中.我提出的语法最直接的方法是:
char* buf;
sprintf(buf, ...);
std::string s(buf);
Run Code Online (Sandbox Code Playgroud)
还有其他想法吗?
我对一些基本的字符串实现有点困惑.我一直在寻找内心的工作和学习新事物.我无法完全掌握如何管理内存.
只是来自基本字符串实现的一些花絮
原始分配器用于char类型
typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
Run Code Online (Sandbox Code Playgroud)...然后,当分配Rep放置在分配的缓冲区内时,__size计算出也适合字符
size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
_Rep *__p = new (__place) _Rep;
Run Code Online (Sandbox Code Playgroud)这是从_Rep缓冲区中获取字符数据的方式
_CharT* _M_refdata() throw()
{
return reinterpret_cast<_CharT*>(this + 1);
}
Run Code Online (Sandbox Code Playgroud)设置角色 - 用于一种方式
_M_assign(__p->_M_refdata(), __n, __c);
Run Code Online (Sandbox Code Playgroud)困扰我的是原始分配器是char类型,但分配的内存可能包含_Rep对象,加上字符数据(不必是char类型)
另外,为什么(或者更确切地说)如何调用以_M_refdata知道字符数据的开始(或结束)在缓冲区内(即this+1)
编辑:this+1只是将内部指针推到_Rep对象后面的下一个位置?
我对内存对齐和转换有基本的了解,但这似乎超出了我所读过的内容.
任何人都可以帮忙,或者给我指出更多信息丰富的阅读材料?
我偶然发现了一个我无法理解的错误.
我认为它基本上归结为这个错误:
error: no matching function for call to ‘std::basic_ostream<char>::operator<<(const std::basic_string<char>&)’
Run Code Online (Sandbox Code Playgroud)
我看着在规范www.cplusplus.com确实也说,没有定义std::ostream::operator<<与std::string作为参数.
我的问题是,当一个人写作时会发生什么std_ostream_instance << std_fancy_string;.我相信它是最常见的调用之一(例如std::out << std::string("Hello world!"))const char*.
错误源自以下行:
template<typename T>
void Log::_log(const T& msg)
{ _sink->operator<<( msg ); }
Run Code Online (Sandbox Code Playgroud)
_sink因为std::ostream*
有一些包装功能,但它在这里打破了.
我想我可以通过写作来解决
template<>
void Log::_log<std::string>(const std::string& msg) {
_sink->operator<<( msg.c_str() );
}
Run Code Online (Sandbox Code Playgroud)
因为ostream& operator<< (ostream& out, const unsigned char* s );默认定义.
我只是看不出为什么它没有被自动猜测,因为它显然可以在简单的使用中工作cout << any_std_string.
不确定这是否相关,但我希望能够通过我的日志功能传递任何可以处理的内容std::ostream.我使用了明确的非模板化声明,但决定转向模板以log(const T& anything_to_log) …
我有一个地图,将一对两个类映射到一个简单的字符串."FirstCollection"和"SecondCollection"是类,"myCollecttion"是其中之一的对象.但是当迭代地图时,我收到编译错误:
错误:调用'(const std :: basic_string)()'不匹配
typedef std::map <
std::pair < Collection, Envelope::Envelope >
, std::string > NameMap;
NameMap globalNameMap = map_list_of
( std::make_pair ( FirstCollection, Envelope::A ), "Something")
( std::make_pair ( SecondCollection, Envelope::B ), "Another thing")
NameMap::const_iterator iter
= globalNameMap.find( std::make_pair ( myCollection, myEnvelope ));
if ( iter == globalNameMap.end() )
{
parent->setName("anything");
} else {
parent->setName(iter->second());
}
Run Code Online (Sandbox Code Playgroud)
这行错误: parent->setName(iter->second());
有什么建议?
我刚刚注意到我在我的C++ 11应用程序中使用了以下代码(它运行得很安静):
template <typename T>
std::string output_streamable_to_string(T const& kObject) {
std::ostringstream os;
os << kObject;
return os.str();
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:标准库(std)中是否存在提供此功能的函数?
我知道std::to_string存在,但只适用于标准库数据类型.我想将a转换boost::asio::ip::udp::endpoint为a std::string.既然boost::asio::ip::udp::endpoint只提供了一个operator<<函数,我需要将其转换std::ostream为a std::string.
考虑以下初始化:
std::string falseString = false;
std::string trueString = true;
Run Code Online (Sandbox Code Playgroud)
有了g++ 5.2.0,编译器抛出警告falseString,而错误trueString.
随着clang++ 3.6 -std=c++11,编译器会引发错误都falseString还有trueString.
Q1)为什么不同的行为gcc即使两个初始化值都是相同的类型(bool)?
Q2)哪个编译器是正确的,为什么?标准说什么?
编辑:
错误:没有可行的从'bool'转换为'std :: string'(又名'basic_string')
警告:将'false'转换为'std :: __ cxx11 :: basic_string <_CharT,_Traits,_Alloc> :: basic_string(const _CharT*,const _Alloc&)的参数1的指针类型[与_CharT = char; _Traits = std :: char_traits; _Alloc = std :: allocator]'[ - Wconversion-null]