我有一张叫做的桌子provider
.我有三个列person
,place
,thing
.可能存在重复的人,重复的地方和重复的事物,但是永远不会存在重复的人物 - 事物 - 组合.
我如何使用这三列在ALTER TABLE中为MySQL中的此表添加复合主键?
如何在PHP中将数组中的所有值转换为小写?
有点像array_change_key_case
?
请解释我的strtok()
功能工作.手册说它把字符串分成了令牌.我无法从手册中了解它实际上是做什么的.
我添加了手表str
并*pch
检查其工作情况,当第一个while循环发生时,内容str
仅为"this".如何在屏幕上显示下面显示的输出?
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Splitting string "- This, a sample string." into tokens: This a sample string
我遇到的代码来自一个似乎认为在结果为负时从另一个相同类型的整数中减去无符号整数的问题.因此,即使它恰好适用于大多数体系结构,这样的代码也是不正确的.
unsigned int To, Tf;
To = getcounter();
while (1) {
Tf = getcounter();
if ((Tf-To) >= TIME_LIMIT) {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我能找到的C标准中唯一含糊不清的引用.
涉及无符号操作数的计算永远不会过度流动,因为无法用结果无符号整数类型表示的结果将以比结果类型可以表示的最大值大1的数量为模.
我想人们可以接受这个引用来表示当右操作数较大时,操作被调整为在模数截断数字的上下文中有意义.
即
0x0000 - 0x0001 == 0x 1 0000 - 0x0001 == 0xFFFF
而不是使用依赖于实现的签名语义:
0x0000 - 0x0001 ==(无符号)(0 + -1)==(0xFFFF但也是0xFFFE或0x8001)
哪种或哪种解释是对的?是否定义了?
我知道这at()
比[]
它的边界检查慢,这也在类似的问题中讨论,如C++ Vector at/[] operator speed 或:: std :: vector :: at()vs operator [] << Surprising results !! 速度慢5到10倍!.我只是不明白这种at()
方法有什么用处.
如果我有一个像这样的简单向量:std::vector<int> v(10);
我决定通过使用at()
而不是[]
在我有索引的情况下访问它的元素i
并且我不确定它是否在向量范围内,它迫使我用try-catch包装它块:
try
{
v.at(i) = 2;
}
catch (std::out_of_range& oor)
{
...
}
Run Code Online (Sandbox Code Playgroud)
虽然我能够通过自己使用size()
和检查索引来获得相同的行为,这对我来说似乎更容易和方便:
if (i < v.size())
v[i] = 2;
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
使用vector :: at over vector :: operator []有什么好处?
我什么时候应该使用vector :: at而不是vector :: size …
我正在尝试学习C而我正在尝试编写一个基本的堆栈数据结构,但我似乎无法获得基本malloc
/ free
正确.
这是我一直在使用的代码(我只是在这里发布一小部分来说明一个特定的问题,而不是总代码,但错误消息是通过运行此示例代码生成的valgrind
)
#include <stdio.h>
#include <stdlib.h>
typedef struct Entry {
struct Entry *previous;
int value;
} Entry;
void destroyEntry(Entry entry);
int main(int argc, char *argv[])
{
Entry* apple;
apple = malloc(sizeof(Entry));
destroyEntry(*(apple));
return 0;
}
void destroyEntry(Entry entry)
{
Entry *entry_ptr = &entry;
free(entry_ptr);
return;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它valgrind
时--leak-check=full --track-origins=yes
,我收到以下错误:
==20674== Invalid free() / delete / delete[] / realloc()
==20674== at 0x4028E58: free (vg_replace_malloc.c:427)
==20674== by 0x80485B2: destroyEntry (testing.c:53)
==20674== by 0x8048477: …
Run Code Online (Sandbox Code Playgroud) 最近我一直在问编写一个函数读取二进制文件到std::vector<BYTE>
哪里BYTE
是unsigned char
.我很快就找到了这样的东西:
#include <fstream>
#include <vector>
typedef unsigned char BYTE;
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<BYTE> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
Run Code Online (Sandbox Code Playgroud)
这似乎是不必要的复杂,并且char*
我在呼叫时被迫使用的明确演员file.read
并没有让我感觉更好.
另一种选择是使用std::istreambuf_iterator
:
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
// read the data:
return std::vector<BYTE>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()); …
Run Code Online (Sandbox Code Playgroud) 我读了一些遗留代码:
if ( 1 || !Foo() )
Run Code Online (Sandbox Code Playgroud)
是否有任何理由不写:
if ( !Foo() )
Run Code Online (Sandbox Code Playgroud) 最近我创建了课程Square
:
=========头文件======
class Square
{
int m_row;
int m_col;
public:
Square(int row, int col): m_row(row), m_col(col)
};
Run Code Online (Sandbox Code Playgroud)
========== cpp文件======
#include "Square.h"
Square::Square(int row, int col)
{
cout << "TEST";
}
Run Code Online (Sandbox Code Playgroud)
但后来我收到很多错误.如果我删除cpp文件并将头文件更改为:
=========头文件======
class Square
{
int m_row;
int m_col;
public:
Square(int row, int col): m_row(row), m_col(col) {};
};
Run Code Online (Sandbox Code Playgroud)
它没有错误.这是否意味着初始化列表必须出现在头文件中?
以下代码每秒输出一个随机数:
int main ()
{
srand(time(NULL)); // Seeds number generator with execution time.
while (true)
{
int rawRand = rand();
std::cout << rawRand << std::endl;
sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
我如何调整这些数字的大小,使它们总是在0-100的范围内?