所以我有一个非常基本的例子,通过https与facebook服务器交谈,但是valgrind正在悲伤地抱怨.所以我假设我没有错误地设置一些东西......有谁知道我做错了什么?
这是我的代码:
#include <string>
#include <iostream>
#include <curl/curl.h>
size_t write_fn_impl( void* ptr, size_t size, size_t nmemb, void * data )
{
std::string * result = static_cast<std::string*>(data);
*result += std::string( (char*)ptr, size*nmemb );
return size*nmemb;
}
int main()
{
std::string url_full="https://graph.facebook.com/me";
std::string useragent = "Facebook API C++ Client (curl)";
CURL * ch_ = curl_easy_init();
char error_buffer[CURL_ERROR_SIZE];
curl_easy_setopt( ch_, CURLOPT_ERRORBUFFER, error_buffer );
curl_easy_setopt( ch_, CURLOPT_WRITEFUNCTION, &write_fn_impl );
std::string result;
curl_easy_setopt( ch_, CURLOPT_WRITEDATA, &result );
int id = 1;
curl_easy_setopt( ch_, CURLOPT_VERBOSE, id …Run Code Online (Sandbox Code Playgroud) 我正在将Java嵌入到C++应用程序中.作为其中的一部分,我需要向Java公开本机函数,以及从C++调用java函数.
我是否需要将我想要调用的函数放入共享库中?或者它们可以以某种方式编译到主机应用程序中?
这是我到目前为止所尝试的内容,但它提供了一个java.lang.UnsatisfiedLinkError
汇编
我正在使用OS X 10.5构建
g++ -Wall -I/System/Library/Frameworks/JavaVM.framework/Headers/ -framework JavaVM -g test.cpp
Run Code Online (Sandbox Code Playgroud)
Java测试文件:TestObject.java
// To build this you need to do a `javac TestObject.java`
// To get the signatures do a `javap -d TestObject`
// To generate the .h file do a `javah TestObject`
public class TestObject
{
public native TestObject get_property( String k );
}
Run Code Online (Sandbox Code Playgroud)
C++测试文件:test.cpp
#include <jni.h>
#include <assert.h>
JNIEXPORT jobject JNICALL Java_TestObject_get_1property(JNIEnv * jni_env, jobject obj, jstring key)
{
//Just a stub implementation for now. …Run Code Online (Sandbox Code Playgroud) 我有一个我想要放入地图的值类型.它有一个很好的默认复制构造函数,但没有默认的构造函数.
我相信只要我远离使用operator[],一切都会好的.
但是我最终得到了像这样的非常丑陋的结构来实际插入一个对象.(我认为如果已经存在该键的值,则插入失败).
// equivalent to m[5]=x but without default construction
std::map<int,X>::iterator it = m.find(5);
if( it != m.end() )
{
m->second = x;
}
else
{
m->insert( std::make_pair(5,x) );
}
Run Code Online (Sandbox Code Playgroud)
我相信它会扫描地图两次,看起来也很难看.
是否有更简洁/更有效的方法来做到这一点?
Kotlin代码是这样的:
class Foo {
companion object {
fun a() : Int = 1
}
fun b() = a() + 1
}
Run Code Online (Sandbox Code Playgroud)
可以简单地改为
object FooStatic {
fun a() : Int = 1
}
class Foo {
fun b() = FooStatic.a()
}
Run Code Online (Sandbox Code Playgroud)
我知道伴侣对象可以用作真正的java静态函数,但使用伴侣对象还有其他优点吗?
我问过一个关于生产者/消费者代码过于笼统的问题(尽管答案肯定是有帮助的).所以我从另一位作者的早期SO问题中得到了建议,并将它们转换为C++并进行了提升.但是我总是对多线程代码有点担心 - 所以如果有人能看到任何明显的改进,我很乐意听到它们.
#include <pthread.h>
#include <deque>
#include <iostream>
#include "boost/thread.hpp"
class MyQueue
{
protected:
boost::mutex mutex_;
boost::condition_variable condition_;
bool cancel_;
std::deque<int> data_;
public:
MyQueue() : mutex_(), condition_(), cancel_(false), data_()
{
}
struct Canceled{};
void push( int i )
{
boost::lock_guard<boost::mutex> l(mutex_);
if(cancel_) throw Canceled();
data_.push_back(i);
condition_.notify_all();
}
void pop( int & i )
{
boost::unique_lock<boost::mutex> l(mutex_);
while(! cancel_ && data_.size()==0 )
{
condition_.wait( l );
}
if(cancel_) throw Canceled();
assert( data_.size() != 0 );
i …Run Code Online (Sandbox Code Playgroud) 我有一个使用 scons 构建的大型 C++ 项目。它的构建速度很慢,我想进行一些更改以使其构建速度更快。现在我想集中时间加速构建中最慢的部分。
如何找出哪些文件的编译时间最长?
我有一些数据存储在有序矢量中.此向量按某个键排序.我知道STL有一个算法来检查一个元素是否在这个排序列表中.这意味着我可以这样写:
struct MyData { int key; OtherData data; };
struct MyComparator
{
bool operator()( const MyData & d1, const MyData & d2 ) const
{
return d1.key < d2.key;
}
};
bool isKeyInVector( int key, const std::vector<MyData> &v )
{
MyData thingToSearchFor;
thingToSearchFor.key = key;
return std::binary_search( v.begin(), v.end(), thingToSearchFor, MyComparator() );
}
Run Code Online (Sandbox Code Playgroud)
但是我发现"thingToSearchFor"对象的构造不够优雅.有没有更好的办法?有类似的东西吗?
struct MyComparator2
{
bool operator()( const MyData & d1, const MyData & d2 ) const
{
return d1.key < d2.key;
}
};
bool isKeyInVector2( int key, …Run Code Online (Sandbox Code Playgroud) 所以我在python中得到了一些我需要使用regexp解析的输入.
目前我正在使用这样的东西:
matchOK = re.compile(r'^OK\s+(\w+)\s+(\w+)$')
matchFailed = re.compile(r'^FAILED\s(\w+)$')
#.... a bunch more regexps
for l in big_input:
match = matchOK.search(l)
if match:
#do something with match
continue
match = matchFailed.search(l)
if match:
#do something with match
continue
#.... a bunch more of these
# Then some error handling if nothing matches
Run Code Online (Sandbox Code Playgroud)
现在我通常喜欢python因为它简洁而简洁.但这感觉很冗长.我希望能够做到这样的事情:
for l in big_input:
if match = matchOK.search(l):
#do something with match
elif match = matchFailed.search(l):
#do something with match
#.... a bunch more of these
else
# …Run Code Online (Sandbox Code Playgroud) 我试图在一个简单的模板类中禁用一些函数.应删除的函数取决于模板参数是否具有某些typedef.
这个例子归结为:
template<typename T>
struct Foo
{
typename T::Nested foo() { return typename T::Nested(); }
int bar() { return 1; }
};
struct NoNested
{
};
struct WithNested
{
typedef int Nested;
};
int main()
{
Foo<WithNested> fwn;
fwn.foo();
fwn.bar();
Foo<NoNested> fnn;
//fnn.foo();
fnn.bar();
}
Run Code Online (Sandbox Code Playgroud)
然而,这给了我error: no type named ‘Nested’ in ‘struct NoNested’gcc和clang ++ 的样式错误(请注意两者的旧版本).
foo当typedef T::Nested没有退出时,是否有一种简单的方法可以删除?(除了Foo<T>类的模板特化之外,在实际代码中我有大约5个具有不同typedef的函数...这将导致2 ^ 5个不同的特化)
编辑: 因为有一些人要求这样做的动机:我想创建类似于编译时间FSM的东西,以便在DSL中使用.
我希望能够做到这一点
struct StateA;
struct StateB;
struct StateC;
struct StateA
{
typedef StateB AfterNext; …Run Code Online (Sandbox Code Playgroud) 从函数返回事件的语法是什么?(不要调用事件,返回它以便它可以绑定到函数).
我有一个容器类,其中包含一个字典,其中每个成员都有一个事件.
目的是能够写出这样的东西:
Container c = new Container();
c.CreateEventForKey("a"); // Create the member in the dictionary
c.EventForKey("a") += some_function; // Bind some_function to the event in the "a" member
c.OnEventForKey("a","b"); // Calls some_function with argument "b"
Run Code Online (Sandbox Code Playgroud)
Container类看起来像这样:
public class Container {
public class Member {
public event Action<string> AnEvent;
public void OnEvent( string v ) { if(AnEvent!=null) { AnEvent(v); } }
}
protected Dictionary<string,Member> members;
// This seems to work OK.
public void OnEventForKey(string k, string v) {
if ( …Run Code Online (Sandbox Code Playgroud)