有没有办法使用argparse创建别名?
例如,我想做这样的事情:
parser.add_argument('--foo' ...)
parser.add_argument_alias('--bar', '--foo')
Run Code Online (Sandbox Code Playgroud)
也就是说,使用--bar应该等同于使用--foo.
我知道这将是一个非常愚蠢的问题,但在阅读了很多关于整个"缓冲"系统的文档之后,我无法理解为什么人们会刷新流而不是缓冲区.
我见过人们写这样的东西:
FILE* file=fopen("mytext.txt","wr");
char buffer[10]="";
setbuf(file,buffer);
//do some stuff....
fflush(file);
....
fclose(file);
Run Code Online (Sandbox Code Playgroud)
所以我想知道,因为我们实际上将事物存储在缓冲区中,为什么我们要刷新与之关联的流而不是直接刷新缓冲区,这实际上存储了一些东西并且应该被刷新.(好吧,有些人告诉我,如果事情发生了就像我说的那样只会是同样的事情所以打扰自己......)
例如,我们不能写像.fflush(buffer)为什么?
我前几天编写了一个小程序来处理单词搜索,并发现,当我为bianry搜索树分配内存时,我存储了我试图分析的每个单词,使用时malloc(),我的4G内存将被快速消耗掉.
我的程序中没有内存韭菜,因为我只为该二进制搜索树分配内存.但是,我仍然可以在程序中分配少于6000个二叉搜索树.二叉搜索树的结构是:
typedef struct BSTnode{
char data[20];
struct BSTnode* leftchild;
struct BSTnode* rightchild;
int num;
}BSTnode;
Run Code Online (Sandbox Code Playgroud)
所以它很小.根据我所学到的,每个结构都需要80个字节的内存(data成本为20个字节,其他因为内存对齐)(对吧?)
因此,内存中的6000个结构总共需要480MB.
但是,当我尝试为6000个结构分配内存时,我的程序失败了(当我为5000分配内存时可以.)我的PC总共有4 GB的内存!(与约1000MB cached,2100MB available和1100MB free(根据Windows上的任务经理)).
这是为什么?
我主要担心的是:
这是为什么?
如何在我的程序中优雅地管理内存分配.
你能提供更多信息吗?(引用,例子和书籍等)
(顺便说一句,如果你想查看我的代码,请在下面留下评论.行数太多,花费一些时间让它更具可读性.抱歉)
################################################## ################## 3码:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct Node
{
struct Node* leftChild;
struct Node* rightChild;
char data[20];
int num;
} Node;
int inputWord(FILE*, Node*);
int main(int argc, char** argv)
{
printf("Enter the name …Run Code Online (Sandbox Code Playgroud) cv2.normalize()我想用如下方式标准化图像
import cv2
original_image = cv2.imread("/path/to/test.jpg")
normalized_image = cv2.normalize(original_image, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX)
Run Code Online (Sandbox Code Playgroud)
这适用于 opencv2(Python)。但使用opencv3时,出现错误
类型错误:未找到所需参数“dst”(位置 2)
被抛出,我必须传递另一个参数
arr = np.array([])
normalized_image = cv2.normalize(original_image, arr, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX)
Run Code Online (Sandbox Code Playgroud)
根据文档,它似乎是目的地,但有趣的是结果存储在normalized_image,并且arr在[]那之后。
那么 opencv3 (Python) 中的第二个参数是什么?
我发现无论如何,我都无法访问外部文件中定义的数组.所以我在c和c ++中做了一些简单的测试:
在C:
main.c中
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int func();
char arr[100]="";
int main()
{
for(int i=0;i<=9;++i){
func();
printf("%s\n",arr);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
func.c
#include<string.h>
#include<stdio.h>
extern char* arr;
int func(){
strcat(arr,"hello"); // try to access arr here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在C++中:
main.cpp中
#include<iostream>
using namespace std;
int func();
char arr[100]="";
int main()
{
for(int i=0;i<=10;++i){
func();
cout<<arr<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
func.cpp
#include<cstring>
extern char* arr;
int func(){
strcat(arr,"hello"); // try to access arr here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你可以看到非常简单的测试.无论我用什么方法访问外部文件中的数组,我都会遇到分段错误.
在功能stricmp()和strnicmp()在C99去除?当我尝试针对C99编译时,我总是得到警告隐式声明函数stricmp()(以及strnicmp()).例如,下面的简单代码让我发出警告.
#include<string.h>
#include<stdio.h>
char arr[100]="hello";
char arr2[100]="hEllo";
int main()
{
int n=-1;
printf("%d\n",n);
n=strnicmp(arr,arr2,3); // the same when use the function stricmp();
printf("%d\n",n);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试针对C99(gcc -Wall -std=c99 main.c -o main)编译这段代码时,我收到了警告.但是当我没有编译它时-std=c99,不会抛出任何警告.但是,即使存在隐式声明的警告,我的代码仍然正常.
这是为什么?那是一个错误吗?如果不是一个错误,那么C99的变化究竟发生了什么?
假设我定义了一个var在主函数中命名的变量。我为它设置了一个观察点。然后我输入另一个名为func(). 此时,观察点可能会被删除,因此我无法访问该变量。有什么方法可以让您随时随地保持观察点?
另外,我知道我可以使用类似的语法print main::var来打印出变量的值。但这还不够。有什么好主意吗?
关于如何std::move()真正空洞的东西,我有些困惑.
我写了一些代码:
int main()
{
string str1("this is a string");
std::cout<<std::boolalpha<<str1.empty()<<std::endl;
string str2(std::move(str1));
cout<<"str1: "<<str1.empty()<<endl;
cout<<"str2: "<<str2.empty()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
假
true //这意味着原始字符串被清空
假
为什么每次都会清空原始字符串?我已经阅读了一些关于移动语义的材料,包括它的原始提议(这一个),它说:
副本和移动之间的区别在于副本保持源不变.另一方面,移动使源处于针对每种类型定义不同的状态.源的状态可以不变,或者可以完全不同.唯一的要求是对象保持自我一致状态(所有内部不变量仍然完好无损).从客户端代码的角度来看,选择move而不是copy意味着您不关心源的状态会发生什么.
所以,根据这个词,上面str1的原始内容应该是某种未定义的.但为什么每次都这样move(),它就被清空了?(实际上我在两者上测试了这个行为std::string,std::vector但结果是一样的.)
要了解更多信息,我将定义自己的字符串类进行测试,如下所示:
class mstring
{
private:
char *arr;
unsigned size;
public:
mstring():arr(nullptr),size(0){}
mstring(char *init):size(50)
{
arr = new char[size]();
strncpy(arr,init,size);
while(arr[size-1] != '\0') //simply copy
{
char *tmp = arr;
arr = new char[size+=50]();
strncpy(arr,tmp,50);
delete tmp;
strncpy(arr-50,init+(size-50),50);
} …Run Code Online (Sandbox Code Playgroud) 我在C中编写程序,可以在一个大的.txt文件中搜索特定的字符串并对其进行计数然后将其打印出来.但似乎出现了问题,导致我的程序输出与两个文本编辑器的输出不同.根据文本编辑器,总共有3000个单词,在这种情况下,我在.txt文件中搜索单词"make" .但我的程序输出只有2970.
我找不到我的程序的问题.所以我很好奇文本编辑器如何准确地搜索特定的字符串?人们如何实现这一点?有人能用C给我看一些代码吗?
为了清楚起见:这是一个大的.txt文件,大约20M,包含很多字符.所以我认为一次将它读入内存并不是那么好.我已经通过将我的程序分成碎片来实现我的程序,然后扫描所有这些以进行解析.但是,它在某种程度上失败了.
也许我应该把代码放在这里.请等一下.
代码有点长,70行左右.我把它放在我的github上,如果您有任何兴趣,请帮忙.https://github.com/walkerlala/searchText 请注意,唯一相关的文件是wordCount.c和testfile.txt,如下所示:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
char arr[51];
int flag=0;
int flag2=0;
int flag3=0;
int flag4=0;
int pieceCount(FILE*);
int main()
{
//the file in which I want to search the word is testfile.txt
//I have formatted the file so that it contain no newlins any more
FILE* fs=fopen("testfile.txt","r");
int n=pieceCount(fs);
printf("%d\n",n);
rewind(fs); //refresh the file...
static bool endOfPiece1=false,endOfPiece2=false,endOfPiece3=false;
bool begOfPiece1,begOfPiece2,begOfPiece3;
for(int start=0;start<n;++start){
fgets(arr,sizeof(arr),fs); …Run Code Online (Sandbox Code Playgroud)