我有以下情况:
我创建了动态库lib.so。该库使用另一个静态库lib.a。它们都使用 Boost 库(我将它们链接到 CMake 文件中)。(我在Java项目中使用这个动态库)
这是lib.so中file.cpp的代码,它从lib.a调用getFilesFromDirectory()
#include "DrawingDetector.h"
#include "../../DrawingDetection.h"
#include <vector>
#include <boost/filesystem/operations.hpp>
using namespace std;
JNIEXPORT void JNICALL Java_DrawingDetector_detectImage( JNIEnv * env, jobject, jstring jMsg)
{
const char* msg = env->GetStringUTFChars(jMsg,0);
vector<File> filesPic = getFilesFromDirectory(msg,0);
env->ReleaseStringUTFChars(jMsg, msg);
}
Run Code Online (Sandbox Code Playgroud)
这是 lib.a 中 getFilesFromDirectory() 的代码:
#include <string.h>
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include "DrawingDetection.h"
using namespace std;
using namespace boost;
using namespace boost::filesystem;
vector<File> getFilesFromDirectory(string dir, int status)
{
vector<File> files;
path directoty = path(dir);
directory_iterator end;
if (!exists(directoty))
{ …
Run Code Online (Sandbox Code Playgroud) 我需要实现算法,使用模板递归计算两个向量的标量积.
有我的代码:
#include <iostream>
#include <vector>
template<typename T, int Size>
T scalar_product(const std::vector<T> &a, const std::vector<T> &b)
{
return a[Size - 1]*b[Size - 1] + (scalar_product<T, Size - 1>(a, b));
}
template<typename T>
T scalar_product<T, 0>(const std::vector<T> &a, const std::vector<T> &b) // error!
{
return a[0] * b[0];
}
int main()
{
std::vector<int> a(3, 1);
std::vector<int> b(3, 3);
std::cout << scalar_product<int, 3>(a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这种专业化T scalar_product<T, 0>(...)
,我会收到错误" 'scalar_product':非法使用显式模板参数 ".但是,如果我像这个T scalar_product(...)
编译器报告一样删除它,递归将是无限的(因为没有专业化,据我所知).
这里有很多这类问题,但我找不到有用的答案.如何在不使用类的情况下专门化这个功能?先谢谢你了!
我是仿函数主题的新手,所以我希望这个问题具有建设性.
我有一些字符串数组().我需要在仿函数的帮助下计算这些字符串的长度总和.
我的代码:
class LengthFinder{
private:
size_t sum;
public:
LengthFinder():sum(0){}
void operator()(string elem)
{
sum += elem.size();
}
operator int()
{
return sum;
}
};
int main(int argc, char* argv[])
{
vector< string > array;
array.push_back("string");
array.push_back("string1");
array.push_back("string11");
string elem;
int sum = std::for_each(array.begin(), array.end(), LengthFinder(/*??*/));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我应该传递给LengthFinder(),获取每个字符串并取其大小?
我有一个由字节数组表示的整数.
byte[] result = getResult();
resultInt1 = Integer.parseInt(Bytes.toString(result));//1
resultInt2 = Integer.parseInt(result.toString());//2
Run Code Online (Sandbox Code Playgroud)
第一种方式一切正常,但在第二种方法中我捕获NumberFormatException.
这两种方法有什么区别?