我想在结构中使用struct作为值.为什么我必须使用value_type在地图中插入一些东西?
#include <map>
struct myStruct {};
int main()
{
std::map<int,myStruct> myStructMap;
myStruct t;
myStructMap.insert(std::map<int,myStruct>::value_type(1, t)); // OK
myStructMap.insert(1,t);
// Error:
// "no instance of overloaded function 'std::map [...]' matches
// the argument list"
}
Run Code Online (Sandbox Code Playgroud) 几天前我不得不使用C,当使用指针时,我有点意外.
C中的一个例子:
#include <stdio.h>
#include <stdlib.h>
void GetPointer(int* p) {
p = malloc( sizeof(int) );
if(p) {
*p = 7;
printf("IN GetPointer: %d\n",*p);
} else {
printf("MALLOC FAILED IN GetPointer\n");
}
}
void GetPointer2(int* p) {
if(p) {
*p = 8;
printf("IN GetPointer2: %d\n",*p);
} else {
printf("INVALID PTR IN GetPointer2");
}
}
int* GetPointer3(void) {
int* p = malloc(sizeof(int));
if(p) {
*p = 9;
printf("IN GetPointer3: %d\n",*p);
} else {
printf("MALLOC FAILED IN GetPointer3\n");
}
return p;
}
int …Run Code Online (Sandbox Code Playgroud) 我觉得有很多类似的问题,所以如果这是重复的话,我真的很抱歉.但是,我无法找到这个具体问题的答案.
当cin被传递给它时,我很困惑getline是如何工作的,因为我的理解是它应该在每次调用时调用cin.使用我正在阅读的书中的代码时,getline被调用几次,但只发送一个输入.除了在这些getline调用中之外,不会从任何地方调用cin对象.
这里发生了什么?当达到getline时,程序是否只是停在其轨道上并等待输入流传递包含所需分隔符的值?如果是这种情况,那么后续的getline调用是否必须等待,因为输入流已经有包含各自分隔符的数据?我跑了几个测试表明可能就是这种情况.
这是代码:
string firstName;
getline(cin,firstName,',');
string lastName;
getline(cin,lastName,',');
string job;
getline(cin,job,'\n');
cout<<firstName<<" "<<lastName<<" is a "<<job<<endl;;
Run Code Online (Sandbox Code Playgroud)
如果这是一个愚蠢的问题再次抱歉,但我环顾四周,真的找不到答案.提前感谢您提供的任何帮助!
澄清:
此代码为控制台输入"First,Last,Job \n"输出"First Last is a Job"
我正在为我的实验室制作一个新的应用程序,我必须在表单终端中使用一些参数,并且我想将**args值复制到一个字符串中,之后我必须匹配argsfor执行一些前戏操作.
我尝试构建此代码以将**argv值复制到字符串var中,但这是错误:
No source available for "std::string::operator=() at 0x44c1dc"
Run Code Online (Sandbox Code Playgroud)
代码是这样的:
int main(int argc, char **argv)
{
string args[argc-1];
int j=0;
for(int i=2; i<argc-1;i++)
{
j=0;
while(argv[i][j]!='\0')
{
args[i]=args[i]+argv[i][j];
j++;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在与硬件进行交互,这些硬件可以让我在数组中包含各种数据uint32_t.不幸的是,我忘记了如何将其中一个解释为uint64_t.
#include <iostream>
#include <cstdint>
int main()
{
const uint64_t x = 123456789123456789ULL;
// Pack into uint32_t[2]
uint32_t ar[2] = {};
ar[0] = x >> 32;
ar[1] = x & 0xFFFF;
// Unpack into uint64_t
uint64_t y;
y = ar[0];
y = (y << 32) + ar[1];
// The output should be the same!
std::cout << x << ' ' << y << std::endl;
}
// Output: 123456789123456789 123456786224144149
Run Code Online (Sandbox Code Playgroud)
(现场演示)
我犯了什么痛苦的明显错误?
我想更改集合(QSet)中的元素:
for(PSet::iterator pIt = P.begin(); pIt != P.end(); ++pIt)
pIt->xp = 0;
Run Code Online (Sandbox Code Playgroud)
编译器不会让我这样做(“C3892:'var':你不能分配给 const 变量”)。集合迭代器似乎总是恒定的,因为担心更改元素可能会破坏其在集合中的正确位置。
就我而言,PSet 是一组结构,我为其定义了自己的哈希函数:
struct P
{
P(int id, const Data_t& data)
:xp(_INFINITY_)
,id(id)
,data(data){}
int xp;
const int id;
const Data_t data;
};
Run Code Online (Sandbox Code Playgroud)
我的哈希函数不考虑非常量成员 xp,因此我的上述分配对于集合中元素的顺序应该是完全安全的。我不想删除该元素并重新插入它,因为性能实际上是这里的一个问题。
我想我可以使用 const 强制转换,但这会损害我的代码的可读性并且看起来像一个令人讨厌的黑客。我还有其他选择吗?
我正在尝试为简单的整数行创建对象的树结构.这是我的测试用例:
#include <vector>
#include <queue>
#include <iostream>
using std::vector;
using std::queue;
using std::cout;
using std::endl;
struct Node
{
Node(int r, int i)
: id(i)
, row(r)
{}
explicit Node(int i)
: id(i)
, row(0)
{}
Node()
: id(0)
, row(0)
{}
int nextLayer(queue<int>& queue, int& curRow)
{
int nextId = queue.front();
queue.pop();
for (int i = 0; i < children.size(); i++) {
if (children[i].id == nextId) {
if (children[i].row == 0)
return children[i].nextLayer(queue, curRow);
else
return children[i].row - 1;
} …Run Code Online (Sandbox Code Playgroud) 我试图做一个间隔树的粗略抽象实现,但我得到一个奇怪的错误.我返回a的所有函数std::map<ui16, Node>::iterator都给出了错误:
错误C2146:语法错误:缺少';' 在标识符"FOO"之前
FOO函数的名称在哪里.
ui16简直就是一个typedef unsigned short.
有任何想法吗?
#include "stdafx.h"
#include <map>
//Very rough implementation of a specialized interval tree
//TODO: Replace std::map with a custom red-black tree implementation or something similar
template <typename T>
class IntervalTree {
public:
struct Node {
Node(T Data, ui16 Length) : data(Data), length(Length) {}
T data;
ui16 length;
};
inline void clear() {
std::map<ui16, Node>().swap(_tree);
}
inline std::map<ui16, Node>::iterator getInterval(ui16 index) const {
auto it = _tree.lower_bound(index); …Run Code Online (Sandbox Code Playgroud) 我试图搞定基类和纯虚函数.
以下是类和标题:
IUpdatble.h
class IUpdatable
{
public:
virtual void Update(void) = 0;
};
Run Code Online (Sandbox Code Playgroud)
InputHandler.h
#include "IUpdatable.h"
class InputHandler :
public IUpdatable
{
public:
InputHandler();
~InputHandler();
virtual void Update(void);
};
Run Code Online (Sandbox Code Playgroud)
InputHandler.cpp
#include "stdafx.h"
#include "InputHandler.h"
InputHandler::InputHandler()
{
}
InputHandler::~InputHandler()
{
}
InputHandler::Update()
{
}
Run Code Online (Sandbox Code Playgroud)
编译器给我这个错误 InputHandler::Update(){}
错误C2556:'
int InputHandler::Update(void)':重载函数的区别仅在于'void InputHandler::Update(void)'的返回类型
据我所知,纯虚函数声明为void没有参数,再次重载函数的声明方式相同.
我用g ++编译了以下代码并且它有效.
bool keyExists(Obj key){
findIn(key,true,false,false,nullptr,nullptr,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)
我使用clang ++编译它,当程序运行时它冻结了.
我把线改为:
bool keyExists(Obj key){
return findIn(key,true,false,false,nullptr,nullptr,1,0,0);
//findIn(key,true,false,false,nullptr,nullptr,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)
现在它的工作原理.
我想它不应该那样工作.这是Clang的已知错误还是一个特例?