我目前正在使用以下代码在表中插入数据:
<?php
public function saveDetailsCompany()
{
$post = Input::All();
$data = new Company;
$data->nombre = $post['name'];
$data->direccion = $post['address'];
$data->telefono = $post['phone'];
$data->email = $post['email'];
$data->giro = $post['type'];
$data->fecha_registro = date("Y-m-d H:i:s");
$data->fecha_modificacion = date("Y-m-d H:i:s");
if ($data->save()) {
return Response::json(array('success' => true), 200);
}
}
Run Code Online (Sandbox Code Playgroud)
我想返回插入的最后一个ID,但我不知道如何获取它.
亲切的问候!
我们有两个大小的向量取决于运行时间,需要检查它们是否相等 - 仅在较小大小的向量结束后才有不同的元素.我使用std :: equal,但问题是我需要先找到哪个矢量尺寸较小,从而产生额外的代码行:
#include <vector>
#include <iostream>
int main()
{
std::vector<int> a(1000, 3);
std::vector<int> a1(100, 3);
if(a.size() > a1.size())
{
if(std::equal(a1.begin(), a1.end(), a.begin()))
{
std::cout << "Same a gt a1" << std::endl;
}
}
if(a1.size() > a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a1 gt a" << std::endl;
}
}
if(a1.size() == a.size())
{
if(std::equal(a.begin(), a.end(), a1.begin()))
{
std::cout << "Same a = a1" << std::endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
可以改进比较两个向量或仅在较小向量的末尾有所不同的代码吗?
我有两个字母数组,格式如下:
const char plain[26] = {'a','b',....'y','z'} // this is the the full alphabet
const char crypt[26] = {'i','d',....'m','x'}; // this is the the alphabet scrambled
Run Code Online (Sandbox Code Playgroud)
两个数组中的字母顺序可以根据输入而改变.此更改发生在main函数中.
这样做的目的是将字符串的字母与第二个字母匹配,就像加密一样.我将字符与数组值进行比较.所以它看起来像这样(简化)
text[3] = 'yes';
changed[3];
if(text[0] == plain[25]){ //would be done under a for loop so 25 would be a changing integer value
changed[0] = [crypt[25];
}
Run Code Online (Sandbox Code Playgroud)
我的代码完全在main函数下工作.我想提到这样的目的,因为我之前的问题只是由于数组和格式的类型.由于数组被移到外面,我可能会再次遇到这些问题.
现在我想让数组全局化.实际加密发生在不将数组作为变量的函数中.但我希望该功能可以访问它们.
这就是它现在的样子
const char plain[26];
const char crypt[26];
int maint(void){
const char plain[26] = {'a','b',....'y','z'} \\values get changed here
const char crypt[26] = {'i','d',....'m','x'} \\and here …
Run Code Online (Sandbox Code Playgroud) 我开始尝试编写一个函数,该函数将从字符串中删除空格,但是现在,我已经研究了许多其他人针对该问题的解决方案,而我只是想确切地了解代码中出了什么问题/为什么std::cout << t << std::endl;
末尾什么都不输出。
当我std::cout << t[count];
在循环中包含该语句(在下面注释掉的语句)时,它将正确输出到控制台:hereissometext
无空格。当我std::cout << t[0] << std::endl;
结束时,它正确输出h
,t[1]
as e
,t[2]
as r
,依此类推。但是,当我尝试t
在最后输出时,它输出空格,并t.size()
输出0
。
我是编码的新手,如果这是一个完全明显的问题,请原谅我。
std::string s = "here is some text";
std::string t = "";
int count = 0;
for (int i = 0; i < s.size(); i++) {
if (std::isalpha(s[i])) {
t[count]+=s[i];
// std::cout << t[count];
count++;
}
}
std::cout << t << std::endl;
Run Code Online (Sandbox Code Playgroud) 我对以下代码段有疑问:
long l=9223372036854775807L;
float f=static_cast<float>(l);
Run Code Online (Sandbox Code Playgroud)
根据IEEE754,不能完全表示long值。
我的问题是如何处理有损转换:
我知道这个问题 ,将int转换为float时在后台会发生什么,但这不会解决我的问题。
我正在读这本书:“ C von A bis Z”。
有这个例子。
/* ptr14.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Fehler: Funktion gibt die Adresse
* einer lokalen Variablen zurück. */
/* [ Error: Function returns the address of a
a local variable. ] */
// ...
/* Möglichkeit2: Speicher vom Heap verwenden */
/* [ Option2: Use memory from the heap ] */
char *test3(void){
char *buffer = (char *) malloc(10);
strcpy(buffer, "testwert");
return buffer;
}
/* Möglichkeit3: Einen Zeiger …
Run Code Online (Sandbox Code Playgroud) 每当我们在C语言中使用变量参数函数时,我们必须提供参数总数作为第一个参数.有没有什么方法可以在不给出参数总数的情况下使用变量参数创建函数?
[ 评论更新:]
我想使用sum(1,2,3)之类的函数应该返回6.即,不应该有计数器.
我时出现分段错误(核心已转储)getline(cin, node->name)
。
我通过str
在输入函数中声明一个字符串来修复,然后node->name = str
。但是跑到线上cin >> node->year
仍然击中了细分故障。
struct client
{
int code;
string name;
int year;
float maths, physics, chemistry;
struct client *next;
};
struct client* input()
{
struct client *node = (struct client *)malloc(sizeof(struct client));
cout << "Code: ";
cin >> node->code;
cout << "Name: ";
cin.ignore();
getline(cin, node->name);
cout << "Year: ";
cin >> node->year;
cout << "Maths, Physics, Chemistry: ";
cin >> node->maths >> node->physics >> node->chemistry;
node->next = …
Run Code Online (Sandbox Code Playgroud) 我在访问另一个结构中的结构向量时遇到问题。可能有些事情我无法解决...
让我们有这样的结构:
struct Struct_1 {
int Id;
int Mode;
string Name;
};
Run Code Online (Sandbox Code Playgroud)
这样的另一个结构:
struct Record {
int Id;
int StructId;
string Name;
vector<Struct_1> structHolder;
};
Run Code Online (Sandbox Code Playgroud)
现在我需要填写一些结构记录
int recordCount = 10;
vector<Record> recordVector(recordCount);
for(int i = 0; i < recordVector.size(); ++i){
recordVector[i].Id = ...
recordVector[i].StructId = ...
recordVector[i].Name = ...
// till now it is ok
recordVector[i].structHolder[i].Id = ..
recordVector[i].structHolder[i].Mode = ..
// and here it fails when i access vector
}
Run Code Online (Sandbox Code Playgroud)
当我尝试填充structHolder的数据时,它失败并显示“ C ++向量下标超出范围”。有人知道哪里出了问题吗?非常感谢!
我试图将给定整数中的所有0替换为5。
为此,我正在使用sprintf将整数转换为字符串并对字符串进行操作,最后将字符串转换回整数
下面是代码:
#include<stdio.h>
int main() {
int num=0, i=0;
char str[10];
printf("Enter the number: ");
scanf("%d",&num);
sprintf(str,"%d",num);
printf("string str:%s",str);
while(str[i]!='\0')
{
if(str[i]==0)
str[i]=5;
i++;
}
sscanf(str,"%d",&num);
printf("\nBefore replacement: %s", str);
printf("\nAfter replacement: %d", num);
}
Run Code Online (Sandbox Code Playgroud)
我输出错误
有人可以识别并纠正这里的问题。谢谢 :)