当我遇到一个绊脚石时,我只是在C++ 0x中寻找一些新东西:
#include <list>
#include <cstdio>
using namespace std;
template <typename T,typename F>
void ForEach (list<T> l, F f) {
for (typename list<T>::iterator it=l.begin();it!=l.end();++it)
f(*it);
}
int main() {
int arr[] = {1,2,3,4,5,6};
list<int> l (arr,arr+6);
ForEach(l,[](int x){printf("%d\n",x);});
}
Run Code Online (Sandbox Code Playgroud)
不编译.我得到一堆未定义的符号错误.这make是输出:
i386-apple-darwin9-gcc-4.5.0 -std=c++0x -I/usr/local/include -o func main.cpp
Undefined symbols:
"___cxa_rethrow", referenced from:
std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
"operator new(unsigned long)", referenced from:
__gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
"___gxx_personality_v0", referenced from:
___gxx_personality_v0$non_lazy_ptr in ccPxxPwU.o
"___cxa_begin_catch", …Run Code Online (Sandbox Code Playgroud) C#具有泛型函数类型,如Action<T>或Func<T,U,V,...>
随着C++ 0x的出现以及具有模板typedef和可变参数模板参数的能力,似乎这应该是可能的.
对我来说显而易见的解决方案是:
template <typename T>
using Action<T> = void (*)(T);
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于仿函数或C++ 0x lambdas,除此之外,不会编译错误" expected unqualified-id before 'using'"
我的下一次尝试是使用boost :: function:
template <typename T>
using Action<T> = boost::function<void (T)>;
Run Code Online (Sandbox Code Playgroud)
出于同样的原因,这也不能编译.
我唯一的另一个想法是STL样式模板参数:
template <typename T, typename Action>
void foo(T value, Action f) {
f(value);
}
Run Code Online (Sandbox Code Playgroud)
但是这不提供强类型解决方案,并且仅与模板化函数相关.
现在,我将首先承认我不是C++,我更喜欢认为我是,所以很可能有一个我没有看到的明显解决方案.
是否可以在C++中使用C#样式泛型函数类型?
我在几个地方看过这个,为了证实我并不疯狂,我寻找其他的例子.显然,这也可以有其他风格,例如operator+ <>.
然而,我在任何地方都没有看到它是什么,所以我想我会问.
这不是谷歌最简单的事情operator<< <>(:-)
继续我之前的问题,如何将月,日和年<select>合并为一个"日期"值,以便它到达我的PHP脚本
$_POST['date'],在MySQL格式YYYY-MM-DD?
所以,我有两个结构:
struct coordinate {
float x;
float y;
}
struct person {
int id;
coordinate location;
}
Run Code Online (Sandbox Code Playgroud)
以及在坐标上操作的函数:
float distance(const coordinate& c1, const coordinate& c2);
Run Code Online (Sandbox Code Playgroud)
在我的main方法中,我有以下代码:
map<int,person> people;
// populate people
map<int,map<float,int> > distance_map;
map<int,person>::iterator it1,it2;
for (it1=people.begin(); it1!=people.end(); ++it1) {
for (it2=people.begin(); it2!=people.end(); ++it2) {
float d = distance(it1->second.location,it2->second.location);
distance_map[it1->first][d] = it2->first;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在构建时收到以下错误:
stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<coordinate>’:
stl_iterator_base_types.h:129: error: no type named ‘iterator_category’ in ‘struct coordinate’
stl_iterator_base_types.h:130: error: no type named ‘value_type’ in ‘struct coordinate’
stl_iterator_base_types.h:131: …Run Code Online (Sandbox Code Playgroud) 我正在成功使用array_key_exists(),如php.net所述
例:
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
Run Code Online (Sandbox Code Playgroud)
但是,取出值,它不起作用.
<?php
$search_array = array('first', 'second');
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
Run Code Online (Sandbox Code Playgroud)
不确定如何仅仅按键来比较2个数组.
C++:
static void doIp(byte data[])
{
unsigned char j, k;
byte val;
byte buf[8];
byte *p;
byte i = 8;
for(i=0; i<8; i++)
{
val = data[i];
p = &buf[3];
j = 4;
do
{
for(k=0; k<=4; k+=4)
{
p[k] >>= 1;
if(val & 1) p[k] |= 0x80;
val >>= 1;
}
p--;
} while(--j);
}
memcpy(data, buf, 8);
}
Run Code Online (Sandbox Code Playgroud)
C#: ?
我正在尝试为Euclid找到两个数字的GCD的方法编写一个简单的缓存机制:
gcd(a,0) = a
gcd(a,b) = gcd(b, a % b)
Run Code Online (Sandbox Code Playgroud)
请注意gcd(a,b) == gcd(b,a).
对于缓存,我需要找到给定的密钥,(a,b)或者(b,a)使用0 < a < 20和0 < b < 20.
当然,我可以使用key = a*20 + b,或者key = a + b*20,但是那些是不对称的 - 关键(1,5)是不同于(5,1).
我怎么能实现这个?
继续我尝试创建一个DateTime类,我试图在我的函数中存储"epoch"时间:
void DateTime::processComponents(int month, int day, int year,
int hour, int minute, int second) {
struct tm time;
time.tm_hour = hour;
time.tm_min = minute;
time.tm_sec = second;
time.tm_mday = day;
time.tm_mon = month;
time.tm_year = year - 1900;
ticks_ = mktime(&time);
processTm(time);
}
void DateTime::processTm(struct tm time) {
second_ = time.tm_sec;
minute_ = time.tm_min;
hour_ = time.tm_hour;
weekday_ = time.tm_wday;
monthday_ = time.tm_mday;
yearday_ = time.tm_yday;
month_ = time.tm_mon;
year_ = time.tm_year + 1900;
}
Run Code Online (Sandbox Code Playgroud)
对于任意日期processComponents(5,5,1990,1,23,45)(1990年6月6日上午1:23:45),它正确地设置所有值并按预期设置. …