我有一个函数,它检查字符串和字符串重复的次数.
它存储为(例如)字符串是"hello"[h] => 1 [e] => 1 [l] => 2 [o] = 1
每当一封信不止一次出现时,我需要更新它.
我试过用
it->second = it->second+1;
Run Code Online (Sandbox Code Playgroud)
但它不起作用我该怎么办?
完整的代码是
int fn(string a) {
map<char,int> mymap;
for(int i=0;i<a.size();i++)
{
std::map<char, int>::iterator it = mymap.find(i);
if(it!=mymap.end())
{
//say i need to update occurrence from 1 to 2 or 2 to 3...
it->second = it->second+1;//(how can i do that)
}
else
mymap.insert(pair<char,int>(a[i],1));
}
std::map<char,int>::iterator i;
for(i=mymap.begin();i!=mymap.end();i++)
{
cout<<i->first<<i->second;
}
}
Run Code Online (Sandbox Code Playgroud) 我需要找到两个日期之间的差异。假设我有 2017-02-01 - 2017-01-01。两天之间的天数就是输出
$formatted_dt1=Carbon::parse($a->date)->format('Y-m-d');
$formatted_dt2=Carbon::parse($c->dt)->format('Y-m-d');
$date_diff=$formatted_dt1->diffInDays($formatted_dt2);
Run Code Online (Sandbox Code Playgroud)
如果我给出上面的代码,我得到的错误是
FatalThrowableError in ReportsController.php line 67:
Call to a member function diffInDays() on string
Run Code Online (Sandbox Code Playgroud) 我有一个词,我想从一个单词中分割出每个字符.我使用FOR IN循环来分割字符.在分割期间,我想检测第一个和最后一个字符.我使用变量count并将count初始化为0.如果count = 1则它是第一个字符.但同样如何在python中检测FOR IN循环中的最后一个字符
count=0;
for w in word:
count++
if count==1:
#first character
Run Code Online (Sandbox Code Playgroud) 我尝试了一个c ++程序,我在类中声明了一个名为"a"的int类型的变量.并创建了一个名为"b"的函数,在其中我再次声明了一个名为"a"的变量并为其赋值.函数内的变量"a"被视为局部变量.如果我想将值赋给类定义中存在的变量a(不在函数内部),我该怎么办呢?
#include <iostream>
using namespace std;
class a{
public:int a;//need to assign value for this "a" inside the function how can i do it
int b(){
int a=5;
a=7;
cout<<a;
}
};
int main() {
a A;
A.b();
}
Run Code Online (Sandbox Code Playgroud) 我是 Laravel 的新手。我不确定如何在同一控制器内调用其他函数并将处理后的值返回到调用它的函数。我尝试过类似C语言的代码,但代码不起作用
class AgreementsApiController extends Controller
{
public function store($th_id,$mv_id,$wk1_terms,$wk2_terms,$wk3_terms)
{
//make a function call here to add function similar to
$result=add($th_id,$mv_id);
}
public function add($th_id,$mv_id)
{ //process the parameters and return to store function
$r=$th_id+$mv_id;
return $r;
}
}
Run Code Online (Sandbox Code Playgroud) 我在这个laravel插入代码中遇到问题。每当创建新公司时,我都会获得与Company创建时一样的输出,但company_name为0。我不知道为什么会这样。我的代码是
控制器:
public function store(Request $request)
{
$Company = new Company([
'company_name' => $request->input('company_name'),
'head'=>$request->input('head'),
'Email'=>$request->input('email'),
'address'=>$request->input('address'),
'phno'=>$request->input('phno'),
'contact_person'=>$request->input('contact_person')
]);
if ($Company->save()) {
$response = [
'msg' => 'Company Created',
'Company' => $Company
];
return response()->json($response, 201);
}
$response = [
'msg' => 'An Error Occurred',
];
return response()->json($response, 502);
}
Run Code Online (Sandbox Code Playgroud)
我的模型代码是:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
//
protected $table = 'companies';
public $timestamps = false;
protected $primaryKey = 'company_name';
protected $fillable = ['company_name','head','contact_person','phno','email','address'];
} …Run Code Online (Sandbox Code Playgroud)