为什么我们需要在C++程序中使用namespace和include指令?
例如,
#include <iostream>
using namespace std;
int main() {
cout << "Hello world";
}
Run Code Online (Sandbox Code Playgroud)
为什么只有#include或者只是使用"使用命名空间std"并摆脱另一个?
(我想到了与Java的类比,import java.net.*将从java.net导入导入所有东西,你不需要做任何其他事情.)
如何开始在C++中进行单元测试?
我在Java编码时使用了Junit,发现它非常有用.C++中有类似的东西吗?您有什么推荐的吗 ?
我想写一些类似于python zip的东西(http://docs.python.org/2/library/functions.html).zip应该接收不同类型的可变数量的向量,并返回元组的向量,截断为最短输入的长度.
例如
x = [1, 2, 3]
v = ['a', 'b']
Run Code Online (Sandbox Code Playgroud)
我希望输出是一个向量
[ <1, 'a'>, <2, 'b'>]
我如何在C++ 11中执行此操作?
我有一个文件(A.txt),数字上有4列,另一个文件有3列数字(B.txt).我需要解决以下问题:
查找A.txt中的所有行,其第3列的编号显示在B.txt的第3列中的任何位置.
假设我在目录中有很多像A.txt这样的文件.我需要为该目录中的每个文件运行它.
我该怎么做呢?
如何更改下面的代码以允许使用模板化构造函数创建 Base 对象?
struct Base {
template <typename T>
Base(int a) {}
};
int main(int argc, char const *argv[])
{
Base *b = new Base<char>(2);
delete b;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个模板课
template <typename Data>
class C
{
.....
}
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,我依靠编译器让我替换数据类型.我在Data类型的对象上调用方法foo(),goo(),因此我需要提供的方法.
我现在需要用int和string替换我的Data类型.我不想专门化,因为类已经太大了,需要专门化每个方法(只需要很少的代码更改).
我的选择(请告诉我是否有更多)
1)我可以提供int和string的包装类,它们实现方法foo(),goo()等
2)提供一个traits类特性,在提供foo(),goo()(这些是我目前的可替代类)的类的对象上调用foo()或goo(),并为int和string专门化这些类.
问题
1)1 vs 2的相对优点是什么?
2)我的特质类将具有静态方法.traits类也可以使用非静态方法吗?我看到大多数traits类在STL中定义常量.
3)我是否将traits类设为全局类,还是应该将它们作为C类的模板参数传递?
有人可以在这里解释我做错了什么:
#include <future>
#include <iostream>
int main() {
std::future<int> result( std::async([](int m) { return m+1;}, 2));
std::cout << "In main" << std::endl;
std::cout << result.get();
}
Run Code Online (Sandbox Code Playgroud)
使用GCC 4.8或4.9,我得到了分段错误.:
./a.out:
In main
Segmentation fault: 11
: /opt/local/bin/g++-mp-4.8 -v
Using built-in specs.
COLLECT_GCC=/opt/local/bin/g++-mp-4.8
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.8.1/lto-wrapper
Target: x86_64-apple-darwin12
Configured with: ../gcc-4.8.1/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc48 --includedir=/opt/local/include/gcc48 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.8 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.8 --with-gxx-include-dir=/opt/local/include/gcc48/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --disable-cloog-version-check --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc48 4.8.1_0'
Thread model: …Run Code Online (Sandbox Code Playgroud) 我不能用自制软件安装gcc48.安装失败(从配置输出的最后几行开始):
checking for version 0.10 of ISL... no
checking for version 0.11 of ISL... no
configure: error: Unable to find a usable ISL. See config.log for details.
==> Formula
Tap: homebrew/versions
Path: /usr/local/Library/Taps/homebrew-versions/gcc48.rb
==> Configuration
HOMEBREW_VERSION: 0.9.4
HEAD: e2b10438666f41366f596fe762b2455f5761bcd8
CPU: 8-core 64-bit sandybridge
OS X: 10.8.4-x86_64
Xcode: 4.6.3
CLT: 4.6.0.0.1.1365549073
X11: 2.7.4 => /opt/X11
==> ENV
HOMEBREW_CC: clang
MAKEFLAGS: -j1
CMAKE_PREFIX_PATH: /usr/local
CMAKE_INCLUDE_PATH: /opt/X11/include/freetype2:/usr/include/libxml2:/opt/X11/include
CMAKE_LIBRARY_PATH: /opt/X11/lib
PKG_CONFIG_PATH: /usr/local/opt/isl/lib/pkgconfig:/usr/local/opt/cloog/lib/pkgconfig
PKG_CONFIG_LIBDIR: /usr/lib/pkgconfig:/usr/local/Library/ENV/pkgconfig/10.8:/opt/X11/lib/pkgconfig:/opt/X11/share/pkgconfig
HOMEBREW_MAKE_JOBS: 1
ACLOCAL_PATH: /usr/local/share/aclocal:/opt/X11/share/aclocal
OBJC: cc
PATH: /usr/local/Library/ENV/4.3:/usr/local/opt/cloog/bin:/opt/X11/bin:/usr/bin:/bin:/usr/sbin:/sbin
Error: gcc48 …Run Code Online (Sandbox Code Playgroud) 我有一大堆日志行,我需要解析每一行(因此效率非常重要).
每个日志行都是表单
cust_name time_start time_end(IP或URL)*
所以ip地址,时间,时间和一个可能是空的ip地址列表或用分号分隔的url.如果最后一个列表中只有ip或url,则没有分隔符.如果超过1,则用分号分隔.
我需要一种方法来解析这一行并将其读入数据结构.time_start或time_end可以是系统时间或GMT.cust_name也可以有多个以空格分隔的字符串.
我可以通过逐字符阅读并基本上编写自己的解析器来实现这一点.有一个更好的方法吗 ?
我需要构建一个具有以下功能的网站:1)用户论坛,我们期望轻松的日常流量2)数据库后端,供用户创建配置文件,他们可以登录和上传媒体(图片)3)用户可以使用他们的个人资料从在线库存中购买内容4)为在线库存创建网页,购物车等5)安全的在线信用卡处理
我对python非常熟悉,但不熟悉python web框架.我知道一些SQL.我如何开始开发这样的东西?Django是一个很好的选择吗?
不是编程相关的本身:你在哪里推荐我为这样的应用程序获得域名的虚拟主机?
我想声明一个模板如下:
template <typename T>
{
if objects of class T have method foo(), then
const int k=1
else
if class has a static const int L then
const int k=L
else
const int k=0;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?一般来说,我想要一种基于T(或T中定义的typedef)属性设置静态consts的机制.
以下是示例输出:
$echo "0.0.0.0 : 0.0.0.0" | awk -F':' '{print $1==$2}'
0
Run Code Online (Sandbox Code Playgroud)
为什么字符串不相等?我需要做些什么来让awk思考
c++ ×7
templates ×3
awk ×2
c++11 ×2
asynchronous ×1
bash ×1
constructor ×1
equality ×1
gcc ×1
grep ×1
header-files ×1
homebrew ×1
if-statement ×1
logging ×1
macos ×1
namespaces ×1
parsing ×1
python ×1
string ×1
text-parsing ×1
traits ×1
unit-testing ×1