以下代码一次读取一个字符的文本文件并将其打印到stdout:
#include <stdio.h>
int main()
{
char file_to_open[] = "text_file.txt", ch;
FILE *file_ptr;
if((file_ptr = fopen(file_to_open, "r")) != NULL)
{
while((ch = fgetc(file_ptr)) != EOF)
{
putchar(ch);
}
}
else
{
printf("Could not open %s\n", file_to_open);
return 1;
}
return(0);
}
Run Code Online (Sandbox Code Playgroud)
但是不是打印到stdout [putchar(ch)]而是想在文件中搜索另一个文本文件中提供的特定字符串,即.strings.txt并输出匹配到out.txt的行
text_file.txt:
1993 - 1999 Pentium 1997 - 1999 Pentium II 1999 - 2003 Pentium III 1998 - 2009 Xeon 2006 - 2009 Intel Core 2
strings.txt:
Nehalem AMD Athlon Pentium
在这种情况下,三个第一text_file.txt行将匹配.我已经对C语言中的文件操作做了一些研究,似乎我可以使用fgetc[像我在我的代码中一样] …
我无法找到这个问题的确切答案,因此在此发布.当我想到vector时,它需要在连续的内存位置构建对象.这意味着向量保持内存分配,并且必须对被推入其中的对象进行就地构造(= placement new).这是一个有效的假设吗?此外,这是否意味着容器手动调用析构函数而不是调用delete?我在这里缺少任何其他假设吗?这是否意味着我可以假设,如果我选择写,即使是为对象编写的自定义新内容也可能无法调用?
此外,列表使用new和delete也是有意义的,因为我们不需要连续的内存保证.那么,这种行为是驱动分配器行为的原因吗?请帮忙.谢谢
我使用的unordered_map是包括的:
#include <unordered_map>
并且程序编译如下:
g++ Test.cc -std=gnu++0x -o test
我使用的unordered_map是TR1还是C++ 0x的.还是两者都一样?
块中的new表达式在我的计算机中try引发bad_alloc异常.
请注意,catch子句按值接收异常对象,而不是通过引用.e.what()打印"bad allocation"怎么样?我以为会被切成碎片.
#include <iostream>
int main()
{
try
{
int* p = new int[0x1F000000];
}
catch(std::exception e)
{
std::cout << e.what() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 在C中获取数组元素的通常方法是这样的:
#define COUNTOF(arr) (sizeof(arr) / sizeof(arr[0]))
Run Code Online (Sandbox Code Playgroud)
这导致了一个整数常量表达式,这也是一个非常好的加法.
问题是它不是类型安全的:int* i; COUNTOF(i); /* compiles :( */.在实践中,这应该很少出现,但为了正确起见,使这种类型安全是很好的.
在C++ 03中这很容易(在C++ 11中它更容易,留给读者练习):
template <typename T, std::size_t N>
char (&countof_detail(T (&)[N]))[N]; // not defined
#define COUNTOF(arr) (sizeof(countof_detail(arr)))
Run Code Online (Sandbox Code Playgroud)
这使用模板推导来获取N数组的大小,然后将其编码为类型的大小.
但是在C中我们没有得到那种语言功能.这是我做的小框架:
// if `condition` evaluates to 0, fails to compile; otherwise results in `value`
#define STATIC_ASSERT_EXPR(condition, value) \
(sizeof(char[(condition) ? 1 : -1]), (value))
// usual type-unsafe method
#define COUNTOF_DETAIL(arr) (sizeof(arr) / sizeof(arr[0]))
// new method:
#define COUNTOF(arr) \
STATIC_ASSERT_EXPR(/* ??? */, \
COUNTOF_DETAIL(arr)) …Run Code Online (Sandbox Code Playgroud) Boost带有一个示例文件
boost_1_41_0\libs\function_types\example
叫interpreter.hpp和interpreter_example.hpp
我试图创建一种情况,我有一堆不同的参数,返回类型等函数所有寄存器并记录到一个位置.然后有能力拉出一个函数并用一些参数执行它.
在这里阅读了几个问题之后,从其他几个问题来看,我认为在这个示例文件中实现的设计与我能够获得的一样好.它需要任何类型的函数,并允许您使用字符串参数列表调用它,该列表被解析为正确的数据类型.基本上它是一个控制台命令解释器,这可能是它的意思.
我一直在研究代码并试图获得相同的实现来接受类成员函数,但到目前为止都没有成功.我想知道是否有人可以建议所需的修改,或者可能在类似的东西上工作并且有相同的代码.
在示例中,您将看到
interpreter.register_function("echo", & echo);
interpreter.register_function("add", & add);
interpreter.register_function("repeat", & repeat);
Run Code Online (Sandbox Code Playgroud)
我想做点什么
test x;
interpreter.register_function("classFunc", boost::bind( &test::classFunc, &x ) );
Run Code Online (Sandbox Code Playgroud)
但这打破了任意数量的论点特征.所以我在想某种自动生成boost :: bind(&test :: classFunc,&x,_1,_2,_3 ......)就是票,我只是不确定实现它的最佳方法.
谢谢
我有一段C代码,用于C++函数.在我的C++文件的顶部,我有一行:#include "prediction.h"
在prediction.h我有这样的:
#ifndef prediction
#define prediction
#include "structs.h"
typedef struct {
double estimation;
double variance;
} response;
response runPrediction(int obs, location* positions, double* observations,
int targets, location* targetPositions);
#endif
Run Code Online (Sandbox Code Playgroud)
我也有prediction.c,它有:
#include "prediction.h"
response runPrediction(int obs, location* positions, double* observations,
int targets, location* targetPositions) {
// code here
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的C++文件中(正如我所说的包括prediction.h)我调用该函数,然后编译(通过Xcode)我得到这个错误:
"runPrediction(int,location*,double*,int,location*)",引自:
MainFrame中的mainFrame :: respondTo(char*,int)
ld:未找到符号
collect2:ld返回1退出状态
prediction.c被标记为当前目标的编译.我没有任何其他.cpp文件没有被编译的问题.这有什么想法?
我确实阅读了一些相关的主题,但问题仍然不明确:
#include <stdio.h>
#include <vector>
#include <iostream>
template <> class stack <int>
{
public:
std :: vector <int> stackVector;
};
Run Code Online (Sandbox Code Playgroud)
编译错误:
templateSpecializ.cpp:5: error: ‘stack’ is not a template
templateSpecializ.cpp:6: error: explicit specialization of non-template ‘stack’
Run Code Online (Sandbox Code Playgroud)
从这个链接:coderSource.net
我错过了一些观点吗?我觉得我有.我甚至试图在那里定义函数,但这没有用.
parallel_for_each 具有以下形式:
Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);
Run Code Online (Sandbox Code Playgroud)
但parallel_for也有类似的形式:
Concurrency::parallel_for(start_value, end_value, function_object);
Run Code Online (Sandbox Code Playgroud)
那么究竟是什么之间的差异Concurrency::parallel_for,并Concurrency::parallel_for_each在编程中使用多个内核算法?
c++ concurrency multicore visual-studio-2010 concurrent-programming