- 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/
据称内联 std::inner_product() 不会内联 gcc 编译器 inline T inner_product(T1 first1, T1 last1, T2 first2, T ini
我想出了我自己的小类,叫做 TinyVector。现在我正在尝试在其上使用 std::inner_product。但我无法让它工作,我不明白为什么这不起作用。我正在使用 Visual Studio 2
看来我在这里做的事情非常错误。你能帮助我吗?目的是将 inner_product 用于复杂 vector 。 #include #include #include #include using n
考虑以下场景: std::vector a {1, 2, 3}; std::vector b {1, 2, 3, 4}; auto it = b.begin(); auto res = std::in
在数字滤波 C++ 应用程序中,我使用 std::inner_product (使用 std::vector 和 std::deque )为每个数据样本计算滤波器系数和输入数据之间的点积。在分析我的应
我知道如果您处理两个不同大小的数组,std::inner_product 算法会出现问题。是否有另一种标准库算法可以处理不同大小的数组,例如通过自动使用两个数组大小中较小的一个? 最佳答案 实现起来并
这是我天真的点积实现: float simple_dot(int N, float *A, float *B) { float dot = 0; for(int i = 0; i <
下面的 C++ 程序应该返回一个严格的正值。但是,它返回 0。 会发生什么?我怀疑是 int-double 转换,但我不知道为什么以及如何。 #include #include #include
我正在尝试创建一个函数来计算标准偏差。 我尝试使用 std::inner_product和用于内部产品 op1 和 op2 的 lambda 表达式来修改 std::inner_product 的操作
以下代码: $ cat test02.cpp #include #include #include #include #include struct myadd : public s
是否可以将 C++ 中的 std::inner_product() 与 omp.h 库并行化?不幸的是,我不能使用在较新版本的 gcc 中可用的 __gnu_parallel::inner_produ
我无法理解如何使用 inner_product结合 std::vector和一个 std::vector> .给定,例如 和 ,> , 我想要 inner_product生产 2* + 3* = +
我想做这样的事情: vector v; v.push_back(0); v.push_back(1); v .transform([](auto i) { return i + 2; })
我想为一个奇怪的问题寻求帮助。对于从网上获得的关于矩阵的代码,我可以在 g++ 上编译和运行,但无法在 VC++ 2008 上构建。vc++ 的构建错误是 ------ Build started:
我对 std::inner_product() 与手动点积计算相比如何执行很感兴趣,所以我做了一个测试。 std::inner_product() 比手动实现快 4 倍。我觉得这很奇怪,因为没有那么多
今天,我想分享一些在尝试实现这个简单操作时让我大吃一惊的事情: 我发现了执行相同操作的不同方法: 通过使用 std::inner_product。 实现谓词并使用 std::accumulate 函数
我是一名优秀的程序员,十分优秀!