我使用macports编译并安装了gcc4.4.
当我尝试使用 - > g ++ -g -Wall -ansi -pthread -std = c ++ 0x main.cpp ...进行编译时:
#include <thread>
...
std::thread t(handle);
t.join();
....
Run Code Online (Sandbox Code Playgroud)
编译器返回:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
但std::cout <<...编译好..
谁能帮我?
当我尝试在Visual Studio 2008中编译时,为什么会出现链接器错误
#include <stdafx.h>
#include <iostream>
#include <map>
#include <string>
class MyClass
{
public:
MyClass () { };
virtual ~MyClass() {};
static std::string niceString (std::map<int, int> mappp) { _myMap = mappp; return "nice string"; };
private:
static std::map<int, int> getMap ( ) { return _myMap; };
static std::map<int, int> _myMap;
};
int main(){
std::map<int, int> mappp;
mappp[1] = 1;
std::cout << MyClass::niceString(mappp);
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Error 1 error LNK2001: unresolved external symbol "private: static class std::map<int,int,struct std::less<int>,class std::allocator<struct std::pair<int const …Run Code Online (Sandbox Code Playgroud) 问题几乎在标题中.根据C++ Reference,std::endl实际上是一个函数.看看它的声明<iostream>,这可以得到验证.
但是,当您使用时std::endl,您不使用std::endl().相反,你使用:
std::cout << "Foo" << std::endl;
Run Code Online (Sandbox Code Playgroud)
实际上,如果您使用std::endl(),编译器需要更多参数,如上面的链接所示.
有人会解释这个吗?有什么特别之处std::endl?我们可以在调用时实现不需要任何括号的函数吗?
从a转换为a的最佳方法std::array<char, N>是std::string什么?
我尝试过生成模板方法,但没有运气.我认为我的C++技能不是最新的.在C++中有这样的惯用方法吗?
如果存在std::map<std::pair<std::string, std::string>, some_type>找到其值的最佳方法是什么?
我想最明显的一个是做这样的事情:
但这将导致在对构造期间对和字符串map.find(std::make_pair(str1, str2));
进行复制构造。str1str2
我希望也许map.find(std::make_pair(std::ref(str1), std::ref(str2)));能有所帮助,但不幸的是没有,这仍然会产生字符串副本。
map.find(std::make_pair(std::move(str1), std::move(str2))应该可以工作,但我们假设这些字符串 ( str1, str2) 是 const 或者不应该移动。
所以我问是否有其他方法可以进行地图搜索而不进行多余的字符串副本?
(请注意,使用std::string_viewforstd::map key不是一个选项,因为地图应该拥有其字符串。)
标准C库函数strtof并strtod具有以下签名:
float strtof(const char *str, char **endptr);
double strtod(const char *str, char **endptr);
Run Code Online (Sandbox Code Playgroud)
它们将输入字符串分解str为三部分:
如果endptr不是NULL,则*endptr设置为指向紧跟在转换的一部分的最后一个字符之后的字符的指针(换句话说,尾随序列的开始).
我想知道:endptr那么,为什么指向非const char指针?不是*endptr指向const char字符串的指针(输入字符串str)?
我有一个向量,我想写它并将其读取到一个文件但是不可能使用sizeof运算符确定向量的逻辑大小.
那我该怎么办?
我有一个std::map叫myMap我的C++应用程序,我想用要么得到一个元素myMap.find(key)或myMap[key].但是,我还想在地图中获取该元素的索引.
std::map<string, int> myMap;
// Populate myMap with a bunch of items...
myElement = myMap["myKey"];
// Now I need to get the index of myElement in myMap
Run Code Online (Sandbox Code Playgroud)
有干净的方法吗?
谢谢.
例如,使用元组:
#include <tuple> // std::tuple, std::make_tuple, std::tie
int num;
char letter;
std::tuple<int,char> num_letter;
num_letter = std::make_tuple(10, 'a');
std::tie(num, letter) = num_letter; // unpack num_letter into num and letter
Run Code Online (Sandbox Code Playgroud)
有没有相当于对的东西?
// ...
num_letter = std::make_pair(10, 'a');
std::pair_tie(num, letter) = num_letter;
Run Code Online (Sandbox Code Playgroud) C++ 11介绍std::mutex及其扩展版本 - std::timed_mutex.
但是,在c ++ 14中我们有std::shared_timed_mutex,但它的'父',std::shared_mutex将在c ++ 17中添加.
对此有任何合理的解释吗?
如果我不打算使用"定时"功能std::shared_timed_mutex,它会比提议更糟糕(更慢,消耗更多资源)std::shared_mutex吗?