- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想为一个奇怪的问题寻求帮助。对于从网上获得的关于矩阵的代码,我可以在 g++ 上编译和运行,但无法在 VC++ 2008 上构建。vc++ 的构建错误是
------ Build started: Project: Matrix, Configuration: Debug Win32 ------
Compiling...Matrix.cpp
c:\matrix\matrix\matrix.cpp(260) : warning C4996: 'std::inner_product': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
c:\program files (x86)\microsoft visual studio 9.0\vc\include\numeric(103) : see declaration of 'std::inner_product'
c:\matrix\matrix\matrix.cpp(275) : warning C4996: 'std::inner_product': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
c:\program files (x86)\microsoft visual studio 9.0\vc\include\numeric(103) : see declaration of 'std::inner_product'
c:\matrix\matrix\matrix.cpp(288) : warning C4018: '<' : signed/unsigned mismatch
c:\matrix\matrix\matrix.cpp(290) : warning C4018: '<' : signed/unsigned mismatch
c:\matrix\matrix\matrix.cpp(346) : warning C4018: '<' : signed/unsigned mismatch
c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(1598) : error C2665: 'std::_Debug_range2' : none of the 2 overloads could convert all the argument types
c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(1577):
could be 'void std::_Debug_range2<_InIt>(_InIt,_InIt,const wchar_t *,unsigned
int,std::input_iterator_tag)'
with
[
_InIt=Cslice_iter<double>
]
c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(1583):
or 'void std::_Debug_range2<_InIt>(_RanIt,_RanIt,const wchar_t *,unsigned
int,std::random_access_iterator_tag)'
with
[
_InIt=Cslice_iter<double>,
_RanIt=Cslice_iter<double>
]
while trying to match the argument list '(Cslice_iter<T>, Cslice_iter<T>, const wchar_t *, unsigned int, double)'
with
[
T=double
]
c:\program files (x86)\microsoft visual studio 9.0\vc\include\numeric(63) : see reference to function template instantiation 'void std::_Debug_range<_InIt1>(_InIt,_InIt,const wchar_t *,unsigned int)' being compiled
with
[
_InIt1=Cslice_iter<double>,
_InIt=Cslice_iter<double>
]
c:\program files (x86)\microsoft visual studio 9.0\vc\include\numeric(106) : see reference to function template instantiation '_Ty std::_Inner_product<Cslice_iter<T>,_InIt2,_Ty,std::_Iter_random_helper<_Cat1,_Cat2>::_Iter_random_cat>(_InIt1,_InIt1,_InIt2,_Ty,_InItCats,std::_Range_checked_iterator_tag)' being compiled
with
[
_Ty=double,
T=double,
_InIt2=double *,
_Cat1=double,
_Cat2=std::random_access_iterator_tag,
_InIt1=Cslice_iter<double>,
_InItCats=std::_Iter_random_helper<double,std::random_access_iterator_tag>::_Iter_random_cat
]
c:\matrix\matrix\matrix.cpp(260) : see reference to function template instantiation 'double std::inner_product<Cslice_iter<T>,_Ty*,double>(_InIt1,_InIt1,_InIt2,_Ty)' being compiled
with
[
T=double,
_Ty=double,
_InIt1=Cslice_iter<double>,
_InIt2=double *
]
Build log was saved at "file://c:\Matrix\Matrix\Debug\BuildLog.htm"
Matrix - 1 error(s), 5 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
非常感谢任何帮助。
非常感谢。
// Program to test slices and a simple N*M matrix class
#include<iostream>
#include<valarray>
#include<algorithm>
#include<numeric> // for inner_product
using namespace std;
// forward declarations to allow friend declarations:
template<class T> class Slice_iter;
template<class T> bool operator==(const Slice_iter<T>&, const Slice_iter<T>&);
template<class T> bool operator!=(const Slice_iter<T>&, const Slice_iter<T>&);
template<class T> bool operator< (const Slice_iter<T>&, const Slice_iter<T>&);
template<class T> class Slice_iter {
valarray<T>* v;
slice s;
size_t curr; // index of current element
T& ref(size_t i) const { return (*v)[s.start()+i*s.stride()]; }
public:
Slice_iter(valarray<T>* vv, slice ss) :v(vv), s(ss), curr(0) { }
Slice_iter end() const
{
Slice_iter t = *this;
t.curr = s.size(); // index of last-plus-one element
return t;
}
Slice_iter& operator++() { curr++; return *this; }
Slice_iter operator++(int) { Slice_iter t = *this; curr++; return t; }
T& operator[](size_t i) { return ref(i); } // C style subscript
T& operator()(size_t i) { return ref(i); } // Fortran-style subscript
T& operator*() { return ref(curr); } // current element
friend bool operator==<>(const Slice_iter& p, const Slice_iter& q);
friend bool operator!=<>(const Slice_iter& p, const Slice_iter& q);
friend bool operator< <>(const Slice_iter& p, const Slice_iter& q);
};
template<class T>
bool operator==(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return p.curr==q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
template<class T>
bool operator!=(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return !(p==q);
}
template<class T>
bool operator<(const Slice_iter<T>& p, const Slice_iter<T>& q)
{
return p.curr<q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
//-------------------------------------------------------------
// forward declarations to allow friend declarations:
template<class T> class Cslice_iter;
template<class T> bool operator==(const Cslice_iter<T>&, const Cslice_iter<T>&);
template<class T> bool operator!=(const Cslice_iter<T>&, const Cslice_iter<T>&);
template<class T> bool operator< (const Cslice_iter<T>&, const Cslice_iter<T>&);
template<class T> class Cslice_iter
{
valarray<T>* v;
slice s;
size_t curr; // index of current element
const T& ref(size_t i) const { return (*v)[s.start()+i*s.stride()]; }
public:
Cslice_iter(valarray<T>* vv, slice ss): v(vv), s(ss), curr(0){}
//typedef T iterator_category; //I added these defs, otherwise more compile errors on vc++
//typedef T value_type; //after I uncomment these, build only fails at call inner_product
//typedef T* iterator; //and transform calls.
//typedef const T* const_iterator;
//typedef T& reference;
//typedef const T& const_reference;
//typedef T* pointer;
//typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
Cslice_iter end() const
{
Cslice_iter t = *this;
t.curr = s.size(); // index of one plus last element
return t;
}
Cslice_iter& operator++() { curr++; return *this; }
Cslice_iter operator++(int) { Cslice_iter t = *this; curr++; return t; }
const T& operator[](size_t i) const { return ref(i); }
const T& operator()(size_t i) const { return ref(i); }
const T& operator*() const { return ref(curr); }
friend bool operator==<>(const Cslice_iter& p, const Cslice_iter& q);
friend bool operator!=<>(const Cslice_iter& p, const Cslice_iter& q);
friend bool operator< <>(const Cslice_iter& p, const Cslice_iter& q);
};
template<class T>
bool operator==(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
{
return p.curr==q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
template<class T>
bool operator!=(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
{
return !(p==q);
}
template<class T>
bool operator<(const Cslice_iter<T>& p, const Cslice_iter<T>& q)
{
return p.curr<q.curr
&& p.s.stride()==q.s.stride()
&& p.s.start()==q.s.start();
}
//-------------------------------------------------------------
class Matrix {
valarray<double>* v; // stores elements by column as described in 22.4.5
size_t d1, d2; // d1 == number of columns, d2 == number of rows
public:
Matrix(size_t x, size_t y); // note: no default constructor
Matrix(const Matrix&);
Matrix& operator=(const Matrix&);
~Matrix();
size_t size() const { return d1*d2; }
size_t dim1() const { return d1; }
size_t dim2() const { return d2; }
Slice_iter<double> row(size_t i);
Cslice_iter<double> row(size_t i) const;
Slice_iter<double> column(size_t i);
Cslice_iter<double> column(size_t i) const;
double& operator()(size_t x, size_t y); // Fortran-style subscripts
double operator()(size_t x, size_t y) const;
Slice_iter<double> operator()(size_t i) { return column(i); }
Cslice_iter<double> operator()(size_t i) const { return column(i); }
Slice_iter<double> operator[](size_t i) { return column(i); }// C-style subscript
Cslice_iter<double> operator[](size_t i) const { return column(i); }
Matrix& operator*=(double);
valarray<double>& array() { return *v; }
};
inline Slice_iter<double> Matrix::row(size_t i)
{
return Slice_iter<double>(v,slice(i,d1,d2));
}
inline Cslice_iter<double> Matrix::row(size_t i) const
{
return Cslice_iter<double>(v,slice(i,d1,d2));
}
inline Slice_iter<double> Matrix::column(size_t i)
{
return Slice_iter<double>(v,slice(i*d2,d2,1));
}
inline Cslice_iter<double> Matrix::column(size_t i) const
{
return Cslice_iter<double>(v,slice(i*d2,d2,1));
}
Matrix::Matrix(size_t x, size_t y)
{
// check that x and y are sensible
d1 = x;
d2 = y;
v = new valarray<double>(x*y);
}
Matrix::~Matrix()
{
delete v;
}
double& Matrix::operator()(size_t x, size_t y)
{
return column(x)[y];
}
//-------------------------------------------------------------
double mul(const Cslice_iter<double>& v1, const valarray<double>& v2)
{
double res = 0;
for (size_t i = 0; i<v2.size(); i++) res+= v1[i]*v2[i];
return res;
}
valarray<double> operator*(const Matrix& m, const valarray<double>& v)
{
if (m.dim1()!=v.size()) cerr << "wrong number of elements in m*v\n";
valarray<double> res(m.dim2());
for (size_t i = 0; i<m.dim2(); i++) res[i] = mul(m.row(i),v);
return res;
}
// alternative definition of m*v
//valarray<double> operator*(const Matrix& m, valarray<double>& v)
valarray<double> mul_mv(const Matrix& m, valarray<double>& v)
{
if (m.dim1()!=v.size()) cerr << "wrong number of elements in m*v\n";
valarray<double> res(m.dim2());
for (size_t i = 0; i<m.dim2(); i++) {
const Cslice_iter<double>& ri = m.row(i);
res[i] = inner_product(ri,ri.end(),&v[0],double(0));
}
return res;
}
valarray<double> operator*(valarray<double>& v, const Matrix& m)
{
if (v.size()!=m.dim2()) cerr << "wrong number of elements in v*m\n";
valarray<double> res(m.dim1());
for (size_t i = 0; i<m.dim1(); i++) {
const Cslice_iter<double>& ci = m.column(i);
res[i] = inner_product(ci,ci.end(),&v[0],double(0));
}
return res;
}
Matrix& Matrix::operator*=(double d)
{
(*v) *= d;
return *this;
}
ostream& operator<<(ostream& os, Matrix& m)
{
for(int y=0; y<m.dim2(); y++)
{
for(int x=0; x<m.dim1(); x++)
os<<m[x][y]<<"\t";
os << "\n";
}
return os;
}
//-------------------------------------------------------------
void f (int x_max, int y_max) // test layout and basic access
{
cout << "\nf(" << x_max <<"," << y_max << "):\n";
Matrix a(x_max, y_max);
for(int x=0; x<x_max; x++) // initialize
for(int y=0; y<y_max; y++)
a[x][y]=x+y*10;
cout<<"C-style access used to initialize:\n" << a;
for(int x=0; x<x_max; x++)
for(int y=0; y<y_max; y++)
a(x,y)=x+y*10;
cout <<"Fortran-style access used to initialize:\n" << a;
cout << "addresses: \n";
for(int x=0; x<x_max; x++)
for(int y=0; y<y_max; y++)
cout<<"("<< x<<","<<y<<") at "<<&a[x][y]-&a[0][0]<<"\n";
cout <<"columns :\n";
for(int x=0; x<x_max; x++) {
cout << "column " << x << ":\n";
for (Slice_iter<double> c = a.column(x); c!=c.end(); ++c)
cout << "\t" << *c <<"\n";
}
cout <<"rows :\n";
for(int y=0; y<y_max; y++) {
cout << "row " << y << ":";
for(Slice_iter<double> r = a.row(y); r!=r.end(); ++r)
cout << "\t" << *r ;
cout <<"\n";
}
}
ostream& operator<<(ostream& os, const valarray<double>& v)
{
for (int i = 0; i<v.size(); ++i) os << '\t' << v[i];
return os;
}
void g(int x_max,int y_max) // check multiplication
{
cout << "\ng(" << x_max <<"," << y_max << "):\n";
Matrix a(x_max,y_max);
for(int x=0; x<x_max; x++) // initialize
for(int y=0; y<y_max; y++)
a[x][y]=x+y*10;
valarray<double> r(2,x_max);
cout << "a*v: " << a*r << endl;
cout << "m*v: " << mul_mv(a,r) << endl;
valarray<double> c(2,y_max);
cout << "v*a: " << c*a << endl;
}
int main()
{
f(3,4);
f(4,3);
g(3,4);
g(4,3);
}
//main for vc++
/*int _tmain(int argc, _TCHAR* argv[])
{
f(3,4);
f(4,3);
g(3,4);
g(4,3);
return 0;
}*/
// the stdafx.h from vc++
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
/*#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
*/
最佳答案
我相信该库试图通过查找行为良好的迭代器应包含的嵌套 typedef 来确定您的迭代器是 input_iterator 还是 random_access_iterator。特别是 iterator_category
。
标准建议从 std::iterator
继承以获得适当的定义。
24.4.2 Basic iterator
Theiterator
template may be used as a base class to ease the definition of required types for new iterators.
namespace std {
template<class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
struct iterator {
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
}
或者您可以将同一组 typedef 添加到您现有的类中。
关于c++ - G++/MSVC++2008 在矩阵实现中调用 inner_product() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484015/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!