小编Cor*_*mer的帖子

C++ 11 - 基于范围的二维地图循环

我有一个二维地图,我这样说:

typedef std::map<std::string, std::map<std::string, Objective>> objectives_t;
Run Code Online (Sandbox Code Playgroud)

我想将这个2d地图的内容保存到文件中.

所以我尝试了类似的东西,受到我在网络上找到的一些代码的启发:

for (auto const &subject : m_objectives) {
    for (auto const &objective : m_objectives[subject.first]) {
        //Print the objective
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当然,这不起作用.我该怎么办?我不确定什么是主题和目标(他们是一些迭代器吗?).

在第二行,我得到:

error: passing 'const objectives_t {aka const std::map<std::basic_string<char>, std::map<std::basic_string<char>, Objective> >}' as 'this' argument of 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = std::map<std::basic_string<char>, Objective>; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::map<std::basic_string<char>, Obj|
Run Code Online (Sandbox Code Playgroud)

c++ dictionary multidimensional-array auto c++11

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

std :: array查找最大值函数

这是我的阵列

std::array<double, 64> fm_sim;
Run Code Online (Sandbox Code Playgroud)

我想找到数组中的最大值.

我不能用

double maxFmSim = std::max(fm_sim.begin(), fm_sim.end());
Run Code Online (Sandbox Code Playgroud)

这是错误:预期的标识符

现在这就是我正在做的事情

 double maxFmSim = fm_sim[0];
 for (int i = 0; i < 64; i++)
 {
    if(fm_sim[i] > maxFmSim)
    {
        maxFmSim = fm_sim[i];
     }
 }
Run Code Online (Sandbox Code Playgroud)

是否有更快的方式/其他std/stl函数,我可以使用它来找到最大值?

c++ arrays max

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

runge kutta第二种方法的python代码是什么?

这段代码可以吗?

def rKN(x, fx, n, hs):
    k1 = []
    k2 = []
    k3 = []
    k4 = []
    xk = []
    for i in range(n):
        k1.append(fx[i](x)*hs)
    for i in range(n):
        xk.append(x[i] + k1[i]*0.5)
    for i in range(n):
        k2.append(fx[i](xk)*hs)
    for i in range(n):
        xk[i] = x[i] + k2[i]*0.5
    for i in range(n):
        k3.append(fx[i](xk)*hs)
    for i in range(n):
        xk[i] = x[i] + k3[i]
    for i in range(n):
        k4.append(fx[i](xk)*hs)
    for i in range(n):
        x[i] = x[i] + (k1[i] + 2*(k2[i] + k3[i]) + …
Run Code Online (Sandbox Code Playgroud)

python math runge-kutta

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

C++,为什么多维数组中一个元素的增加似乎在增加另一个?

这可能不太优雅.主要是因为我对C++比较陌生,但是这个我正在整理的小程序在这里磕磕绊绊.

我不明白.我误解了数组吗?编辑后的代码是:

int diceArray [6][3][1] = {};

...

}else if (y >= xSuccess || x >= xSuccess){

// from here...

    diceArray[2][1][0] = diceArray[2][1][0] + 1;
    diceArray[2][1][1] = diceArray[2][1][1] + 1;

// ...to here, diceArray[2][2][0] increases by 1. I am not referencing that part of the array at all. Or am I?

}
Run Code Online (Sandbox Code Playgroud)

通过使用评论,我追踪到了第二个表达式的罪魁祸首.如果我发表评论,第一个diceArray[2][2][0]不会改变.

为什么diceArray[2][1][1] = diceArray[2][1][1] + 1导致diceArray[2][2][0]增加?

我试过了..

c = diceArray[2][1][1] + 1;

diceArray[2][1][1] = c;
Run Code Online (Sandbox Code Playgroud)

..作为解决方法,但它是一样的.它增加diceArray[2][2][0]了一个.

c++ arrays

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

根据用户输入c ++打开文件

我正在尝试制作一个程序,根据用户输入打开一个文件.这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

    string filename;
    ifstream fileC;

    cout<<"which file do you want to open?";
    cin>>filename;

    fileC.open(filename);
    fileC<<"lalala";
    fileC.close;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时,它给了我这个错误:

[Error] no match for 'operator<<' (operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and 'const char [7]')
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?谢谢...

c++ io user-input file

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

for循环:C++和python

我理解如何使用for循环

for (x = 0 ; x<5 ; x++ ) { // some_code }
Run Code Online (Sandbox Code Playgroud)

在C++中,但是像for循环一样

for x in y:
Run Code Online (Sandbox Code Playgroud)

至少存在于python中

c++ python for-loop

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

为什么我不能使用if和if else语句为变量赋值?

#include <iostream>
using namespace std;

void main()
{
    int leftover;
    int gold = 3900;//satisfies the if else statement

    if( gold>=4100){//successfully build item
        leftover = gold-4100;
    }
    else if(4100>gold>=3500){
        leftover = gold-3500;
    }

    cout << leftover << endl;
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

此代码不起作用,它将显示剩余使用而未初始化.但是当我改变gold(等等4200)的值以满足if语句时,它会起作用,显示从4100中扣除金币之后的余数.我只是在学校学习c ++所以我不熟悉if和if else声明还有很多感谢告诉我出了什么问题!

c++ variables if-statement

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

Python tkinter函数中的if语句未正确设置变量

下面的代码是计算我正在进行的纸牌游戏得分手的积分.我的问题与我的Round_Points函数中的if/elif语句有关.每当我运行代码时,Trick_Base仅从最后的else语句设置为878(我选择测试的随机数),即使我在GUI中为Tricks_Bid输入6到10之间的值(我正在使用tkinter) Python 3.4).我是否需要添加一些内容以确保将Trick_Base设置为if语句中的适当值?或者我的条目小部件中有什么东西可以关闭吗?到目前为止,我搜索过的任何内容都没有给我任何关于错误的线索.在此先感谢您的任何帮助或建议!

PS:我的代码基于这个示例计算器:http ://www.tkdocs.com/tutorial/firstexample.html当我开始添加if和elif语句时,问题就开始了.

from tkinter import *
from tkinter import ttk

def Round_Points():
    if Tricks_Bid == 10:
        Trick_Base = 400
    elif Tricks_Bid == 9:
        Trick_Base = 300
    elif Tricks_Bid == 8:
        Trick_Base= 200   
    elif Tricks_Bid == 7:
        Trick_Base = 100    
    elif Tricks_Bid == 6:
        Trick_Base = 90   
    else:
        Trick_Base=878

    global Suit_Base

    Points.set(Trick_Base+Suit_Base)

root = Tk()
root.title("Tricks")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

Points = IntVar()
Suit_Base …
Run Code Online (Sandbox Code Playgroud)

python if-statement tkinter function python-3.x

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

for循环中的函数问题

def vol_tank(R,h):
    ht = R + 2.3*R

    if ht >= h:
        vol = 3.14 * R*R * h + 3.14 * R*R * h / 3
        print ('R: %.1f , h: %.1f - volume: %.2f' % (R,h,vol))
    elif R >= h:
        vol = 3.14 * R*R * h / 3
        print ('R: %.1f , h: %.1f - volume: %.2f' % (R,h,vol))
    else:
        print ('R: %.1f , h: %.1f - overflowing' % (R,h))

values = [[0.8, 1, 1.3, 1.5], [1, 1.25, …
Run Code Online (Sandbox Code Playgroud)

python for-loop if-statement function list

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

不能在 dev c++ 5.9 中包含 std 数组

我想在 DEv C++ 5.9 中包含数组,但它给出了一个错误:

32  2   c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\c++0x_warning.h    [Error] #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
Run Code Online (Sandbox Code Playgroud)

这是因为我使用的 DEV-C++ 版本还是我的电脑 IOS?我使用了其他版本的 DEV C++,但由于我的计算机上装有 Windows 8,因此它们无法解决此问题,因此我需要使用此版本的 DEV C++。任何帮助表示赞赏。

谢谢

c++ dev-c++ c++11

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