我正在使用xcode来编辑一些c ++代码 - 我一直在讨厌xcode不能将"string"识别为关键字(即它不会给它带来典型的粉红色'关键字颜色').
有谁知道如何将其添加为关键字?
str.find('X')的反义词 - 在std :: string中找到与特定char不同的第一个字符的最有效方法是什么?如果我有一个主要由X'es组成的字符串,但在某些时候还有另一个字符 - 我如何快速找到它?
使用Microsoft Visual Studio 2012编译器,使用boost 1.53,我有一些使用的工作代码:
#include <boost/regex.hpp>
Run Code Online (Sandbox Code Playgroud)
后来我将以下两行添加到regex.hpp之前包含的头文件中:
namespace std {template<class T> class basic_string<T>;}
typedef std::basic_string<TCHAR> tstring;
Run Code Online (Sandbox Code Playgroud)
现在我得到了一堆编译错误,但如果在这些其他行之前包含regex.hpp,则没有错误.
许多错误的前几个是:
c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2027: use of undefined type 'std::basic_string<_Elem,_Traits,_Alloc>'
19> with
19> [
19> _Elem=char16_t,
19> _Traits=std::char_traits<unsigned short>,
19> _Alloc=std::allocator<char16_t>
19> ]
19>c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2065: 'const_iterator' : undeclared identifier
19>c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2923: 'boost::match_results' : 'const_iterator' is not a valid template type argument for parameter 'BidiIterator'
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2825: '_Iter': must be …Run Code Online (Sandbox Code Playgroud) 我遇到了一个我无法弄清楚的特殊问题:
for ( const auto & p : _patterns )
{
auto it = std::find_if( p->Tokens().begin(),p->Tokens().end(),
[&]( const Token & lhs )
{
return ( lhs == query );
});
if ( it != p->Tokens().end() )
d_freq++;
}
Run Code Online (Sandbox Code Playgroud)
代币是:
std::vector<Token>
Run Code Online (Sandbox Code Playgroud)
并且std :: find_if lambda内部的实际运算符被定义并实现为:
bool Token::operator== ( const Token & rhs ) const
{
if ( !this->_value.empty() && !rhs._value.empty() )
{
return boost::iequals( this->_value, rhs._value );
}
else
throw
std::runtime_error ( "[Token::operator==] empty string" );
}
Run Code Online (Sandbox Code Playgroud)
运行我的调试版本只是使用SEGFAULT崩溃.
只有当我运行gdb,然后回溯时,我得到:
Program received …Run Code Online (Sandbox Code Playgroud) 我有以下测试程序:
#include <string>
#include <iostream>
int main()
{
std::string s;
std::string a = "sd";
std::cout << a==s ? "y" : "n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试编译它g++ test.cpp会产生以下神秘错误:
error: no match for 'operator==' (operand types are 'std::basic_ostream<char>' and 'std::string {aka std::basic_string<char>}')
std::cout << a==s ? "y" : "n";
^
Run Code Online (Sandbox Code Playgroud)
它似乎s被正确编译为类型std::string,而a被编译为std::basic_ostream<char>!?救命!!
我想知道是否有一种方法可以以编程方式禁用字符串SSO,使其不使用本地缓冲区作为短字符串?
请问,为什么我需要这个.我很好,如果不能关闭只是想确定.
以下函数返回一个 char*:
// Returns the pointer to the actual packet data.
// @param pkt_handle The buffer handle.
// @param queue The queue from which the packet is arrived or destined.
// @return The pointer on success, NULL otherwise.
u_char * pfring_zc_pkt_buff_data(
pfring_zc_pkt_buff *pkt_handle,
pfring_zc_queue *queue
);
Run Code Online (Sandbox Code Playgroud)
以下函数将字符串作为输入。
template<class ...Args>
bool write(Args&&... recordArgs) {
auto const currentWrite = writeIndex_.load(std::memory_order_relaxed);
auto nextRecord = currentWrite + 1;
if (nextRecord == size_) {
nextRecord = 0;
}
if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
new (&records_[currentWrite]) …Run Code Online (Sandbox Code Playgroud) std::basic_string::find方法的签名之一是:
size_type find( const CharT* s, size_type pos, size_type count ) const;
Run Code Online (Sandbox Code Playgroud)
参数如下:
pos - 开始搜索的位置
count - 要搜索的子字符串的长度
s - 指向要搜索的字符串的指针
此重载的方法行为的描述是:
查找等于范围 [s, s+count) 的第一个子字符串。此范围可能包含空字符。
我想知道在什么情况下有一个包含空字符的范围会很有用。例如:
s.find("A", 0, 2);
Run Code Online (Sandbox Code Playgroud)
这里,s对应一个长度为1的字符串。因为count是2,范围[s,s+count)包含一个空字符。重点是什么?
一个std::string_view参数比const char*下面代码中的一个参数更好吗?
void func( const std::string_view str )
{
std::istringstream iss( str.data( ) ); // str is passed to the ctor of istringstream
std::size_t pos { };
int num { std::stoi( str.data( ), &pos, 10 ) }; // and here it's passed to std::stoi
}
int main()
{
std::array<char, 20> buffer { };
std::cin.getline( buffer.data( ), 20 );
func( buffer.data( ) );
}
Run Code Online (Sandbox Code Playgroud)
std::istringstreamctor 和都std::stoi需要 aconst std::string&作为参数。但我std::string_view使用其data()成员函数向他们传递一个对象。这是不好的做法吗?我应该回到 …
我必须确定输入是数字还是字符串。
std::string s;
while (std::cin >> s) {
if(isdigit(s)){
//do something with the variable
}
else{
//do something else with the variable
}
}
Run Code Online (Sandbox Code Playgroud)
为此,我得到
error: no matching function for call to 'isdigit(std::__cxx11::string&)'
有人可以提出我应该使用的方法吗?