memset

( 图论专题 )【 最短路 】

橙三吉。 提交于 2019-12-02 15:13:12
( 图论专题 )【 最短路 】 dijkstra的堆优化(最简版本) 先了解不用堆优化的dijkstra:https://blog.csdn.net/weixin_43828245/article/details/90722389 推荐视频讲解(代码是Python写的,重点听思路):https://www.bilibili.com/video/av25829980 了解c++优先队列:https://blog.csdn.net/weixin_43828245/article/details/90742490 #include <iostream> #include <queue> #include <cstring> #define inf 0x3f3f3f using namespace std; typedef struct node { int v,date; } ty; bool operator < ( const ty &a, const ty &b ) // 自定义优先队列规则,先pop出小的来 { return a.date>b.date; // 这里和sort相反,a>b是先pop出小的 } int a[2002][2002]; int via[2002]; // 判断是否已经pop出来了 int dis[2002]; // 存放距离 int i,j,n,m;

Fastest way to zero out a 2d array in C?

…衆ロ難τιáo~ 提交于 2019-12-02 13:55:00
I want to repeatedly zero a large 2d array in C. This is what I do at the moment: // Array of size n * m, where n may not equal m for(j = 0; j < n; j++) { for(i = 0; i < m; i++) { array[i][j] = 0; } } I've tried using memset: memset(array, 0, sizeof(array)) But this only works for 1D arrays. When I printf the contents of the 2D array, the first row is zeroes, but then I got a load of random large numbers and it crashes. memset(array, 0, sizeof(array[0][0]) * m * n); Where m and n are the width and height of the two-dimensional array (in your example, you have a square two-dimensional array, so

SIGSEV on strcmp of memset string

≯℡__Kan透↙ 提交于 2019-12-02 13:34:26
In the following program, i am expecting the for loop to stop after 3 elements. But it keeps on going indefinitely and fails later on with a coredump. is malloc() needed for char*[]` would strcmp fail if i memset to 0? . #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char* str[10]; memset(str,0,10); str[0]=malloc(sizeof(char)*(strlen("Sample")+1)); str[1]=malloc(sizeof(char)*(strlen("Sample")+1)); str[2]=malloc(sizeof(char)*(strlen("Sample")+1)); strcpy(str[0],"Sample"); strcpy(str[1],"Sample"); strcpy(str[2],"Sample"); int i=0; for(i=0;strcmp("",str[i])!=0;i++) { printf(

memset is not working with pointer to character

烂漫一生 提交于 2019-12-02 13:10:48
What is wrong with the following code? memset is supposed to work with Pointer to the block of memory to fill. But this code displays problem in console saying segmentation fault(core dumped) #include<iostream> #include <cstring> using namespace std; int main(int argc, char** argv) { char* name = "SAMPLE TEXT"; memset(name , '*', 6); cout << name << endl; return 0; } You have tripped over a very old backward-compatibility wart in C++, inherited from C and dating to the days when there was no such thing as const . String literals have type const char [n] , but, unless you tell your compiler you

C语言-memset()

眉间皱痕 提交于 2019-12-02 09:43:06
1. memset()函数原型是extern void *memset(void *buffer, int c, int count) buffer:为指针或是数组, c:是赋给buffer的值, count:是buffer的长度. 这个函数在socket中多用于清空数组.如:原型是memset(buffer, 0, sizeof(buffer)) Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’; 例:char a[100];memset(a, ‘/0’, sizeof(a)); memset可以方便的清空一个结构类型的变量或数组。 如: struct sample_struct { char csName[16]; int iSeq; int iType; }; 对于变量: struct sample_strcut stTest; 一般情况下,清空stTest的方法: stTest.csName[0]=’/0’; stTest.iSeq=0; stTest.iType=0; 用memset就非常方便: memset(&stTest,0,sizeof(struct sample_struct)); 如果是数组: struct sample_struct TEST[10]; 则 memset(TEST,0,sizeof

???????

前提是你 提交于 2019-12-02 06:51:42
#include <bits/stdc++.h> using namespace std; const int M=10005; const int N=10005; #define ri register int struct setdian{ int to,val,net; }bian[M]; int head[N],cent,dfn[N],low[N],leve,color[M],tim,stk[M],top,cost[M],ans; void add(int a,int b,int c) { bian[++cent].net=head[a],head[a]=cent; bian[cent].to=b;bian[cent].val=c; // } void dfs1(int u,int f) { low[u]=dfn[u]=++tim; stk[top++]=u; for(ri i=head[u];i;i=bian[i].net) { int v=bian[i].to; if(dfn[v]==0) { dfs1(v,i); low[u]=min(low[u],low[v]); } else if((i^1)!=f) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { leve++; while(top>0) { int v

【简解】SP7556 Stock Charts

人盡茶涼 提交于 2019-12-02 05:47:08
题目大意 给出一个折线图,有N条线段,你想要把这些线段分成几个集合,使得每个集合中任意两条线段不相交。 求最少集合数。 分析 喵帕斯 :以下提及的所有折线均指横坐标在 \([1,k]\) 里的折线段。 思考两个折线若不相交会是什么情况? 对,即一个在上,一个在下(怎么有点奇怪呢)。 比如折线 \(a\) 在上,折线 \(b\) 在下,尝试对所有满足此关系的折线二元组连一条 \(a\) 到 \(b\) 的有向边,我们可以发现,使用一个集合可以走一条路径,那么题目求最小集合数,即求走最少的路径,师所有点全部被覆盖。 然后此题就转化为了 最小路径覆盖问题 ,假设你已经会了该问题,然后就是喜闻乐见的匈牙利模板时间啦~ 不会? 祝好梦。 #include<queue> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define ll long long const int N = 200 + 5; const int M = 10000 + 5; inline int read(){ int f = 1, x = 0; char ch; do { ch = getchar(); if (ch == '-') f = -1; } while (ch <

C++中的内存操作

徘徊边缘 提交于 2019-12-02 05:44:40
1. memcpy # include <string.h> void * memcpy ( void * dest , void * src , unsigned int count ) 将源地址src指向内存区域的count个字节赋值到dest为起始地址的内存区域 src和dest所指内存区域不能重叠,函数返回指向dest的指针 注意,对象不能是简单的内存拷贝,例如C++中STL的字符串类型string,因为memcpy执行的是浅拷贝,只是简单的把第二个内存指向第一个内存的引用,而程序结束时,会对一块内存进行两次内存释放(析构函数)。 C++ String 使用注意: memcpy 2. memset # include <string.h> void * memset ( void * buffer , char c , int count ) 把buffer所指内存区域的前count个字节设置成字符c 返回指向buffer的指针 也适用于整数数组初始化为0或者-1,因为memset按照字节操作,有符号数的0为全0,有符号数的-1为全1,例如 int a [ 100 ] ; memset ( a , 0 , sizeof ( a ) ) ; int b [ 1000 ] ; memset ( b , - 1 , sizeof ( b ) ) ; 3. strcpy #

视频帧双缓冲区的两个版本

丶灬走出姿态 提交于 2019-12-02 05:08:32
视频帧双缓冲区的第一个版本,仅缓冲视频帧,CSharedCriticalSection是临界区保护类 class CFrameCache{ public: CFrameCache() : pop_frame_ptr_(nullptr) , push_frame_ptr_(nullptr) , width_(0), hight_(0), channels_(0){ } ~CFrameCache() { if(pop_frame_ptr_) { delete[] pop_frame_ptr_; pop_frame_ptr_ = nullptr; } if(push_frame_ptr_){ delete[] push_frame_ptr_; push_frame_ptr_ = nullptr; } } void push_frame(const char* frame, int width, int hight, int channels){ if(nullptr == frame || width < 0 || hight < 0 || channels < 0) { return ; } CSharedMutex lock(mutex_); if(push_frame_ptr_ == nullptr && pop_frame_ptr_ == nullptr) { width_ =

模板 - SPFA

守給你的承諾、 提交于 2019-12-02 03:26:55
普通的SPFA: int top; int head[MAXN]; struct Edge { int v, nxt; int w; } edge[MAXM]; void init() { top = 0; memset(head, -1, sizeof(head)); } void add_edge(int u, int v, int w) { ++top; edge[top].v = v; edge[top].w = w; edge[top].nxt = head[u]; head[u] = top; } bool vis[MAXN]; int cnt[MAXN]; int dis[MAXN]; queue<int>q; bool spfa(int s, int n) { memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); for(int i = 0; i < MAXN; ++i) dis[i] = 0x3f3f3f3f; while(!q.empty()) q.pop(); q.push(s); vis[s] = 1; cnt[s] = 1; dis[s] = 0; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i =