小编and*_*dre的帖子

使用operator >>两次后为什么std :: getline失败?

我有给定的输入:

 local
 127.0.0.1 localhost
 other
 next
Run Code Online (Sandbox Code Playgroud)

使用以下代码,输出是一个空白,我期望其他.输出是"输出:"

#include <iostream>
using namespace std;

int main() {
    std::string ip, domain, header;
    std::getline(cin, header);
    cin >> ip >> domain;
    std::getline(cin, header);
    std::cout << "output: " << header;
}
Run Code Online (Sandbox Code Playgroud)

但是,我注意到cin >> ip >> domain;在调用之前提取两次()时会出现此问题std::getline.如果我有的话,代码就像我期望的那样工作cin >> ip.当我使用双提取(operator>>)时,为什么我会看到这个奇怪的结果std::getline

c++ operators getline extraction

3
推荐指数
1
解决办法
209
查看次数

boost :: interprocess :: managed_shared_memory崩溃程序

我的目标是创建一个名为SharedMemory的模板单例类,它可以使用boost :: interprocess :: managed_shared_memory将给定的数据结构存储在共享内存中的映射中.

#ifndef SHARED_MEMORY_H_
#define SHARED_MEMORY_H_

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <string>
#include <utility>
#include <map>

using namespace boost::interprocess;

template<typename T>
class SharedMemory {
  typedef std::pair<std::string, T> ValueType;
  typedef allocator<ValueType, managed_shared_memory::segment_manager>
    ShmemAllocator;
  typedef map<std::string, T, std::less<std::string>, ShmemAllocator> SharedMap;

  public:
    static SharedMemory<T>& instance();
    void Create();
    void Destory();
    void insert(T* t);
    std::map<std::string, T> getMapOfRecords();
  private:
  SharedMemory();
  ~SharedMemory(){delete m_segment;}
  void Initialize();
  managed_shared_memory* m_segment;
  std::size_t m_size;
};

template<typename T>
inline void SharedMemory<T>::Create() {
  Destory();
  m_segment = new …
Run Code Online (Sandbox Code Playgroud)

c++ boost shared-memory boost-interprocess

2
推荐指数
1
解决办法
2873
查看次数

在C++中添加素数的问题

我的代码适用于最多300或20的数字.但它不适用于2000000.我尝试使用long,但它仍然无效.

#include <iostream>
bool prime(int i) {
    bool result = true;
    int isitprime = i;

    for (int j = 2; j < isitprime; j++) { ///prime number tester
        if (isitprime % j == 0) {
            result = false;
            break;
        }
    }

    return result;
}

int main(void) {
    using namespace std;
    long sum = 0;

    for (long i = 2; i <= 2000000; i++) {
        if (prime(i)) {
            sum += i;
        }
    }

    cout << sum << endl;
    system("pause");
    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
233
查看次数

如何在stm32f107芯片上禁用/启用中断?

我有一个ARM stm32f107芯片。我正在将项目从 IAR 移植到 GCC

IAR 提供以下函数来启用和禁用中断:

#define __disable_interrupt() ...
#define __enable_interrupt() ...
Run Code Online (Sandbox Code Playgroud)

如何使用 GCC 启用/禁用我的芯片的中断?

c embedded assembly gcc iar

1
推荐指数
1
解决办法
1万
查看次数

值没有正确递增

此应用程序的目的是模拟大量的掷骰子游戏.我有另一个版本,它玩一个游戏,要求用户输入和输出信息.此版本的目的是仅在10,000次游戏后显示结果.结果是房子赢了多少场比赛,玩家赢了多少场比赛以及每场比赛的平均掷骰数.我还没有实现滚动,因为我想让游戏首先正确递增.

当我执行这个时会发生什么是一个数字墙(这是由于cout << playerwintotal;)并且是故意的,但数字重复3-4次,直到循环已经超过10,000次.

即.1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5等

最终结果通常是这样的:

经过10,000次掷骰子游戏后,
玩家赢得了2502场比赛.
这座房子赢了3625场比赛.

我不确定如何解决这个问题,因为我可以说一切都是应该的,尽管这只是我第四天的C++.

#include <iostream>
#include <string>
#include "randgen.h"

using namespace std;

const int MAX_PLAYS = 10000;

int main() {

    int roll;
    RandGen rg;
    int die1 = rg(6) + 1;
    int die2 = rg(6) + 1;
    int point;
    int total = die1 + die2;
    bool playerwin;
    bool housewin;
    int playerwintotal = 0;
    int housewintotal = 0; …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
112
查看次数

Objective-c:如何完成多个异步服务调用并阻塞它们

我有一个senario,要求我多次调用web api.以下是一个例子.

getDataAsync:(NSDictionary *)dictionary withCompletion: (void (^)(NSDictionary*))completion {

    __block int counter = n; // the number of async blocks
    __block NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    void (^returnBlock)(void) = ^{
        counter--;
        if(counter != 0) return;
        completion(@{@"return": output});
        return;
    };
    void (^getResourceA)(void) = ^{
        [service getResourceA : dictionary[@"idA"] completion:
        ^(ServiceResult results, MyResourceA *a, NSString *errMsg) {
            [output setValue:a.value forKey:a.name];
            returnBlock(); 
        }];
    };
    // followed by n-1 other blocks like getResourceA
    //... 
}
Run Code Online (Sandbox Code Playgroud)

我想在这里使用内置dispatch_queue而不是我自己的自定义解决方案.鉴于异步服务调用使用的内部完成块,我该怎么做?

关于如何解决这个问题的任何其他建议将不胜感激.

asynchronous objective-c objective-c-blocks

1
推荐指数
1
解决办法
1071
查看次数

使用std :: find与operator ==时出错

我在以下结构上使用std :: find收到错误...

struct ComplianceOrderRecord {
    explicit ComplianceOrderRecord(IOrder& order);
    bool operator ==(const ComplianceOrderRecord& other) const;
    double price;
};

inline bool ComplianceOrderRecord::operator ==(const ComplianceOrderRecord& other) const {
    return price == other.price;
}
Run Code Online (Sandbox Code Playgroud)

我用它如下......

inline void Compliance::RemoveComplianceOrderRecord(const ComplianceOrderRecord& order) {
    auto it = std::find(m_compliantOrderList.begin(),
    m_compliantOrderList.end(), order);
    if(it == m_compliantOrderList.end()) {
        return;
    }
    m_compliantOrderList.erase(it);
}
Run Code Online (Sandbox Code Playgroud)

错误是......

error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const ComplianceOrderRecord' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

任何帮助理解这个错误将非常感激.

c++ std

0
推荐指数
1
解决办法
917
查看次数

C++中动态的整数列表

我正在尝试在我的List类中创建一个动态数组,该数组将从大小为2开始,当您使用Insert方法插入值时,它将检查是否有足够的空间,否则它将调整数组的大小大小+ 2 ...问题是它崩溃VS正在抱怨堆的损坏.另外我认为我的拷贝构造函数没有被调用,因为cout没有显示:

list.h文件:

class List
{
public:

    //  DEFAULT Constructor
    List();
    // Deconstructor to free memory allocated 
    ~List();// Prevent memory leaks

    // COPY Constructor for pointers
    List(const List& value);// copy constructor

    //Modification methods
    void Insert(const int);

    // User ACCESS methods
    void display(void) const;

private:
    int size;// MAX size of the array          
    int count;// current number of elements in the dynamic array

protected:
    int *intptr;// Our int pointer
};
Run Code Online (Sandbox Code Playgroud)

list.cpp实现文件:

#include "list.h" // Include our Class defintion
#include <iostream>

using …
Run Code Online (Sandbox Code Playgroud)

c++ arrays dynamic copy-constructor

0
推荐指数
1
解决办法
9976
查看次数

如何最小化OnInitDialog函数中的MFC应用程序?

我需要我的MFC应用程序来最小化启动.我对MFC没有多少经验.

到目前为止,我尝试过:删除WS_VISIBLE,调用ShowWindow(SW_HIDE)和使用OnSize(...).每个人都无法工作和/或导致应用程序崩溃.

我的下一个想法是使用该PostMessage功能,但我不确定参数是什么.

有人可以告诉我使用PostMessage函数最小化我的应用程序的参数或给我另一个选项.

c++ mfc visual-studio-2010

0
推荐指数
1
解决办法
2561
查看次数

如何在Python中制作队列的有界图?

我需要制作一个有界队列的字典。以下是我到目前为止所拥有的。

from collections import defaultdict
from collections import deque

d = defaultdict(deque)
...
q = d[name]
Run Code Online (Sandbox Code Playgroud)

创建有界队列:q = deque(maxlen = 10)可以,但我不确定当队列位于字典中时如何执行此操作。谁能帮我制作有界队列的地图?

python queue dictionary

0
推荐指数
1
解决办法
463
查看次数

ISO C++禁止指针和整数之间的比较[-fpermissive]

代码

int cycle_length(int i, int j) {
    int cycleLength = 0;
    for (int k = i; k <= j; k++) {
        cout << algorithm(k) << endl;
        if (algorithm(k) > cycle_length) {
            cycleLength = algorithm(k);
        }
    }
    return cycleLength;
}
Run Code Online (Sandbox Code Playgroud)

ISO C++ forbids comparison between pointer and integer [-fpermissive]

我在这一行得到了这个错误if ( algorithm(k) > cycle_length).

然而,这是怎么回事main()?什么是这个错误意味着???

添加 算法是一个取整数输入并返回整数的函数.

int algorithm(int number1) {
    int counter = 1, number = number1;
    do {
        if (number % 2 == 0) { …
Run Code Online (Sandbox Code Playgroud)

c++ iso

0
推荐指数
1
解决办法
2320
查看次数

任何更好的方法来实现这一点

当我遇到这个算法(进行更改)并在下面实现它时,我正在网上冲浪...但仍然有任何有效的方法来做到这一点......我怎么能从我实施的程序中找到相同的复杂性...

1>算法如下

makechange(c[],n) //c will contain the coins which we can take as our soln choice and 'n' is the amount we want change for
soln<-NULL//set that will hold solution
sum=0
while(sum!=n)
{
    x<-largest item in c such that sum+x<=n
    if(there is no such item)
    return not found
    soln <- soln U {a coin of value x}
    sum=sum+x
    return soln
}

2>here is what i have tried

#include<stdio.h>
#include<conio.h>

void main() {
    int c[]= {100,50,20,10,5,1},soln[6];
    int num,i,j,sum=0,x,k,flag=0;
    clrscr();
    printf("\nEnter amount …
Run Code Online (Sandbox Code Playgroud)

c c++ algorithm

-1
推荐指数
1
解决办法
469
查看次数