我正在阅读Stroustrup的"The C++ Programming Language",他说有两种方法可以向变量添加内容
x = x + a;
Run Code Online (Sandbox Code Playgroud)
和
x += a;
Run Code Online (Sandbox Code Playgroud)
他更喜欢,+=因为它最有可能更好地实施.我认为他的意思是它的工作速度也更快.
但它真的吗?如果它取决于编译器和其他东西,我该如何检查?
我有两个std::map<int,int>地图,并希望将它们合并到第三个地图中:如果在两个地图中找到相同的键,则在第三个地图中使用相同的键创建一对,并使用一个值来表示第一个和第二个的值之和映射,否则只需将一对复制到第三个映射.我怀疑它可以用std::accumulate,但我不太了解它.
我试图实现一种算法来搜索给定数组中的最小和最大元素,并使用Cormen的算法导论中的想法.我的代码编译并开始工作,输出生成的随机数组,然后在很长一段时间内什么都不做.为什么会这样?
代码是这样的:
// fast min and max --cormen exercise 1.cpp: entry point
//implemented from a verbal description in cormen's book, p 243
#include "stdafx.h"
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iostream>
struct min_and_max
{
int min, max;
};
min_and_max find_min_and_max(std::vector<int>& A)
{
int n = A.size();
int min, max;
if (n%2 == 1)
min = max = A[0];
if (n%2 == 0)
if (A[0] < A[1])
{
min = A[0];
max = A[1];
}
else
{
min …Run Code Online (Sandbox Code Playgroud) 我编写了一些代码,用于使用梯形规则以数字方式集成函数.它有效,但它产生的答案有一个错误的标志.为什么会这样?
代码是:
integration :: (Double -> Double) -> Double -> Double -> Double
integration f a b = h * (f a + f b + partial_sum)
where
h = (b - a) / 1000
most_parts = map f (points (1000-1) h)
partial_sum = sum most_parts
points :: Double -> Double -> [Double]
points x1 x2
| x1 <= 0 = []
| otherwise = (x1*x2) : points (x1-1) x2
Run Code Online (Sandbox Code Playgroud)
代码可能不够优雅,但我只是Haskell的学生,并希望首先处理当前的问题,然后编码风格很重要.
我的代码在MS VS 2012中编译后,接受输入,然后崩溃并使用以下报告:
Debug Assertion Failed!
...\include\xstring
Line:1662
Expression:string subscript out of range
Run Code Online (Sandbox Code Playgroud)
代码如下:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <time.h>
using namespace std;
const unsigned short MAX_STRINGS = 10;
const unsigned int MAX_SIZE=10000;
vector<string> strings;
unsigned int len;
string GetLongestCommonSubstring( string string1, string string2 );
inline void readNumberSubstrings();
inline const string getMaxSubstring();
void readNumberSubstrings()
{
cin >> len;
assert(len > 1 && len <=MAX_STRINGS);
strings.resize(len);
for(unsigned int i=0; i<len;i++)
strings[i]=string(MAX_SIZE,0);
for(unsigned int i=0; i<len; …Run Code Online (Sandbox Code Playgroud) 我正在阅读Cormen的"算法简介",我正在尝试实现堆排序,并且有一件事我一直都不理解:我们如何计算heap_size给定数组?我的教科书说
表示堆的数组A是具有两个属性的对象:A.length,(通常)给出数组中元素的数量,和A.heap-size,表示堆中存储的元素数量数组A.也就是说,虽然A [1 .. A.length]可能包含数字,但只包含A [1..A.heap-size]中的元素,其中0 <= A.heap-size <= A.length ,是堆的有效元素.
如果我实现一个数组std::vector<T> Arr,那么它的'大小将是Arr.size,但是它的'heap_size是什么目前超出我的范围.
我有一个功能ip,我无法改变:
ip = show a ++ show b ++ show c ++ show d
Run Code Online (Sandbox Code Playgroud)
我必须定义a,b,c并d因此:
GHCi> ip
"127.224.120.12"
Run Code Online (Sandbox Code Playgroud)
成立.我怎么做?
我正在尝试编写一个Haskell函数,它将获取三个列表并返回其元素总和的列表.目前我正在尝试使用zipWith3:
sum3 :: Num a => [a] -> [a] -> [a] -> [a]
sum3 xs ys zs = zipWith3 (\x y z -> x+y+z) xs ys zs
Run Code Online (Sandbox Code Playgroud)
问题是它只适用于相同长度的列表.但我希望sum3使用不等长的列表,以便这样做
sum3 [1,2,3] [4,5] [6]
Run Code Online (Sandbox Code Playgroud)
会回来的
[11,7,3]
Run Code Online (Sandbox Code Playgroud)
我认为我应该重新定义zipWith3来处理不等长的列表,但是无法弄清楚如何做(我怀疑我必须用尽所有空列表的可能性).有解决方案吗?
我有这个代码,它是为了解决Project Euler的第11个问题,并且它没有进一步工作printout(),之后它崩溃了"堆栈溢出".我猜我的索引有些问题(可能在next_elem函数中),但我看不出是什么.请给一个提示.
#include "stdafx.h"
#include <iostream>
const int matrix[20][20] = {
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, …Run Code Online (Sandbox Code Playgroud) 我正在从文件中读取某些数据到vector<struct>.代码是这样的:
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ifstream fin("gift1.in", ios::in);
ofstream fout("gift1.out", ios::out);
unsigned short NP;
struct person
{
string name;
unsigned int gave;
unsigned int received;
};
vector<person> accounts;
string tmp_name;
fin >> NP;
accounts.resize(NP);
for (auto i : accounts)
{
fin >> tmp_name;
fout << "Just read this name: " << tmp_name << "\n";
i.name = tmp_name;
i.gave = 0;
i.received = 0;
fout << "We …Run Code Online (Sandbox Code Playgroud) 我正在尝试使一对实例Printable,但我无法弄清楚正确的语法.我的Printable是这样的:
class Printable a where
toString :: a -> [Char]
instance Printable Bool where
toString True = "true"
toString False = "false"
instance Printable () where
toString () = "unit type"
Run Code Online (Sandbox Code Playgroud)
和我的对实例是这样的:
instance Printable ( a, b ) where
toString (a,b) = "(" ++ toString a ++ ","++ toString b ++ ")"
Run Code Online (Sandbox Code Playgroud)
在编译时,给了我一个No instance for (Printable a) arising from a use of ‘toString’.我究竟做错了什么?