我试着运行以下代码:
std::map < std::string, std::string > m;
m[ "one" ] = "0";
m[ "two" ] = "1";
m[ "three" ] = "2";
m[ "four" ] = "3";
auto it = m.begin();
std::cout << it->first << "\n";
Run Code Online (Sandbox Code Playgroud)
输出是:"four".但为什么从头到尾呢?我被期待了"one"!
#include<bits/stdc++.h>
#define mpq pair<int,Query>
using namespace std;
class Query{
public:
int a1,a2,b1,b2,c1,c2,d,l,r;
Query(){}
Query(int a1,int a2,int b1,int b2,int c1,int c2,int d,int l,int r){
this->a1=a1; this->a2=a2;
this->b1=b1; this->b2=b2;
this->c1=c1; this->c2=c2;
this->d=d; this->l=l; this->r=r;
print();
}
Query(int d,int l,int r){
Query(0,1,0,1,0,1,d,l,r);
}
void print(){
cout<<d<<" "<<l<<" "<<r<<endl;
}
};
map<int,Query> query;
int main(){
query.insert(mpq(1,Query(0,1,0,1,0,1,1,1,1)));
query[1].print();
cout<<endl;
query[4]=Query(4,4,4);//not working properly
query[4].print();//giving output of query[1].print
cout<<endl;
query[2]=Query(0,1,0,1,0,1,2,2,2);
query[2].print();
}
Run Code Online (Sandbox Code Playgroud)
我无法解释此代码的输出
query[4].print()是否给出了意外的输出
我期待输出
4 4 4
但它正在给予
1 1 1
作为输出
我想做这样的事情:
void func(void *data, const int dtype)
{
typedef typename std::conditional<dtype==0,float,double>::type DataType;
funcT((DataType *)data);
return;
}
Run Code Online (Sandbox Code Playgroud)
这不会编译,因为dtype需要在编译时知道.我试图避免使用switch语句,因为我正在使用8个数据类型,有许多函数,如上面的那些,通过ctypes从Python调用.
有没有像std :: conditional这样的方法可以在运行时完成,利用传入的dtype标识符?
这是代码:
std::vector<Dword> s;
s.push_back(~Dword(0));
Run Code Online (Sandbox Code Playgroud)
什么是~Dword?它是如何工作的?
我正在使用Howard Hinnant的免费,开源,跨平台的C++ 11/14时区库:
#include "date/tz.h"
#include <iostream>
int
main()
{
std::cout << date::make_zoned("Europe/Rome", std::chrono::system_clock::now()) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
我得到这样的输出:
2017-11-29 16:24:32.710766 CET
Run Code Online (Sandbox Code Playgroud)
是否可以从zoned_time提取日期(例如:星期一)和HH:MM的不同字符串?能否zoned_time转换为结构TM?
我有个问题.当我使用带有自定义比较器的std :: set时,其他操作(如erase或count)无法正常工作.例如:
int sz(int const & n) {
return __builtin_popcount(n);
}
struct comp {
bool operator()(int const & a, int const & b) const {
return sz(a) >= sz(b);
}
};
void solve() {
set<int, comp> s;
for (int i = 0; i < 10; ++i)
s.insert(i);
for (int x : s)
cerr << x << " ";
cerr << "\n";
for (int i = 0; i < 10; ++i)
cerr << s.count(i) << " ";
}
Run Code Online (Sandbox Code Playgroud)
输出将是:
7 …Run Code Online (Sandbox Code Playgroud) 我在代码中对std ::函数有些困惑。
cout<<tmp;
cout<<std::hex<<setw(4)<<tmp;
Run Code Online (Sandbox Code Playgroud)
tmp是一个int变量。设置tmp目录六角型,当我再次打印TMP之后,它不是转换回小数类型(在脸上,我必须包括的std :: DEC)。谁能为我解释幕后发生的事情?
提前致谢
当我在没有-std = gnu ++ 11开关的情况下编译时,这个循环工作正常.如果我使用-std = gnu ++ 11进行编译,则会出现以下错误:
no match for call to '(__gnu_cxx::__normal_iterator<const float*, std::vector<float> >) (const float&, const float&)' if (!bool(__binary_pred(*__first1, *__first2)))
Run Code Online (Sandbox Code Playgroud)
我的代码:
for (auto it_A = std::begin(vector_A); it_A != std::end(vector_A); it_A += 4)
{
bool found_in_B = false;
for (auto it_B = std::begin(vector_B); !found_in_B && it_B != std::end(vector_B); it_B += 4)
{
found_in_B = std::equal(it_A, it_A + blockSize, it_B, it_B + blockSize);
}
}
Run Code Online (Sandbox Code Playgroud) 当我写作
std::shared_ptr<MyType> x;
Run Code Online (Sandbox Code Playgroud)
使用指向nullptr的空共享指针初始化x.但我希望它能自动调用MyType的默认(或其他一些指定的构造函数).我知道,我可以写:
std::shared_ptr<MyType> x = std::maked_shared<MyTYpe>();
Run Code Online (Sandbox Code Playgroud)
但有时人们会忘记然后你遇到麻烦,所以编译器强制执行此操作会很好.
我正在尝试从具有std::stringas键的无序映射中接收值,其中一些字符串只包含一个字符.我的所有输入都来自于std::stringstream我从中获取每个值并将其转换为char,然后将其转换为字符串使用
std::string result {1, character};,根据文档和此答案,这似乎是有效的.
但是,当我这样做时,字符串前置\ x01(对应于值1).这使得在地图中找不到字符串.我的调试器还确认字符串大小为2,值为"\ x01H".
为什么会发生这种情况,我该如何解决?
#include <iostream>
#include <sstream>
#include <unordered_map>
int main()
{
const std::unordered_map<std::string, int> map = { {"H", 1}, {"BX", 2} };
std::stringstream temp {"Hello world!"};
char character = static_cast<char>(temp.get()); // character is 'H'
std::string result {1, character}; // string contains "\x01H"
std::cout << result << " has length " << result.size() << std::endl; // H has length 2
std::cout << map.at(result) << std::endl; …Run Code Online (Sandbox Code Playgroud) c++ ×10
std ×10
c++11 ×3
constructor ×2
dictionary ×2
vector ×2
boost ×1
c++17 ×1
conditional ×1
decimal ×1
destructor ×1
hex ×1
iterator ×1
shared-ptr ×1
sstream ×1
stdmap ×1
stdset ×1
string ×1