go tour有这个例子用于频道:https://tour.golang.org/concurrency/2
package main
import "fmt"
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}
Run Code Online (Sandbox Code Playgroud)
在sum函数中修改通道c,并且在函数终止后更改仍然存在.显然c是通过引用传递的,但没有创建指向c的指针.是否通过引用隐式传递了通道?
一个简单的问题,我在这里找不到答案.
我理解的是,在调用期间将参数传递给函数,例如
void myFunction(type myVariable)
{
}
void main()
{
myFunction(myVariable);
}
Run Code Online (Sandbox Code Playgroud)
对于像int,float等等的简单数据类型,函数由值调用.
但是如果myVariable是数组,则只传递起始地址(即使我们的函数是值函数调用).
如果myVariable是对象,则也只传递对象的地址,而不是创建副本并传递它.
所以回到这个问题.C++是通过引用还是值传递对象?
我有一个函数将一个字符串解析为两个字符串.在C#中我会声明它是这样的:
void ParseQuery(string toParse, out string search, out string sort)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我会这样称呼它:
string searchOutput, sortOutput;
ParseQuery(userInput, out searchOutput, out sortOutput);
Run Code Online (Sandbox Code Playgroud)
当前项目必须在C++/CLI中完成.我试过了
using System::Runtime::InteropServices;
...
void ParseQuery(String ^ toParse, [Out] String^ search, [Out] String^ sort)
{
...
}
Run Code Online (Sandbox Code Playgroud)
但如果我这样称呼它:
String ^ searchOutput, ^ sortOutput;
ParseQuery(userInput, [Out] searchOutput, [Out] sortOutput);
Run Code Online (Sandbox Code Playgroud)
我得到一个编译器错误,如果我这样称呼它:
String ^ searchOutput, ^ sortOutput;
ParseQuery(userInput, searchOutput, sortOutput);
Run Code Online (Sandbox Code Playgroud)
然后我在运行时收到错误.我该如何申报和调用我的功能?
我用c ++编写了一个简单的程序来比较两种方法之间的性能 - 按值传递并通过引用传递.实际上,传递的值比通过引用传递更好.
结论应该是通过值传递需要更少的时钟周期(指令)
如果有人能详细解释为什么传递值需要更少的时钟周期,我会很高兴.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void function(int *ptr);
void function2(int val);
int main() {
int nmbr = 5;
clock_t start, stop;
start = clock();
for (long i = 0; i < 1000000000; i++) {
function(&nmbr);
//function2(nmbr);
}
stop = clock();
cout << "time: " << stop - start;
return 0;
}
/**
* pass by reference
*/
void function(int *ptr) {
*ptr *= 5;
}
/**
* pass …Run Code Online (Sandbox Code Playgroud) 可能重复:
Java是"传递引用"吗?
我今天发现了一种不寻常的Java方法:
private void addShortenedName(ArrayList<String> voiceSetList, String vsName)
{
if (null == vsName)
vsName = "";
else
vsName = vsName.trim();
String shortenedVoiceSetName = vsName.substring(0, Math.min(8, vsName.length()));
//SCR10638 - Prevent export of empty rows.
if (shortenedVoiceSetName.length() > 0)
{
if (!voiceSetList.contains("#" + shortenedVoiceSetName))
voiceSetList.add("#" + shortenedVoiceSetName);
}
}
Run Code Online (Sandbox Code Playgroud)
根据我读过的有关Java传递变量,复杂对象的行为的一切,这段代码应该什么都不做.所以嗯......我在这里错过了一些东西吗?是否有一些微妙的东西丢失在我身上,或者这些代码是否属于thedailywtf?
如何在java中通过引用传递原始类型?例如,如何将int传递给可修改的方法?
我想创建一个字符串并通过引用传递它,以便我可以更改单个变量并将其传播到引用它的任何其他对象.
举个例子:
function Report(a, b) {
this.ShowMe = function() { alert(a + " of " + b); }
}
var metric = new String("count");
var a = new Report(metric, "a");
var b = new Report(metric, "b");
var c = new Report(metric, "c");
a.ShowMe(); // outputs: "count of a";
b.ShowMe(); // outputs: "count of b";
c.ShowMe(); // outputs: "count of c";
Run Code Online (Sandbox Code Playgroud)
我希望能够实现这一目标:
var metric = new String("count");
var a = new Report(metric, "a");
var b = new Report(metric, "b");
var c …Run Code Online (Sandbox Code Playgroud) 为什么在创建时不能通过引用传递对象std::thread?
例如,以下snippit给出了编译错误:
#include <iostream>
#include <thread>
using namespace std;
static void SimpleThread(int& a) // compile error
//static void SimpleThread(int a) // OK
{
cout << __PRETTY_FUNCTION__ << ":" << a << endl;
}
int main()
{
int a = 6;
auto thread1 = std::thread(SimpleThread, a);
thread1.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
In file included from /usr/include/c++/4.8/thread:39:0,
from ./std_thread_refs.cpp:5:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(int))(int&)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(int&); _Args = {int&}]’ …Run Code Online (Sandbox Code Playgroud) 我想询问是否可以通过引用将参数传递给脚本函数:
即在C中做一些看起来像这样的事情:
void boo(int &myint) { myint = 5; }
int main() {
int t = 4;
printf("%d\n", t); // t->4
boo(t);
printf("%d\n", t); // t->5
}
Run Code Online (Sandbox Code Playgroud)
那么在BASH我想做的事情如下:
function boo ()
{
var1=$1 # now var1 is global to the script but using it outside
# this function makes me lose encapsulation
local var2=$1 # so i should use a local variable ... but how to pass it back?
var2='new' # only changes the local copy
#$1='new' this is wrong …Run Code Online (Sandbox Code Playgroud) 我用这样的原型编写了一些函数:
template <typename input_iterator>
int parse_integer(input_iterator &begin, input_iterator end);
Run Code Online (Sandbox Code Playgroud)
这个想法是调用者将提供一系列字符,并且该函数会将字符解释为整数值并将其返回,从最后使用的字符开始.例如:
std::string sample_text("123 foo bar");
std::string::const_iterator p(sample_text.begin());
std::string::const_iterator end(sample_text.end());
int i = parse_integer(p, end);
Run Code Online (Sandbox Code Playgroud)
这将begin设置为123并i"指向"之前的空间p.
我被告知(没有解释)通过引用传递迭代器是不好的形式.这是不好的形式?如果是这样,为什么?