www.91084.com

GVKun编程网logo

c – 从内部替换std :: function(通过move-assignment到* this?)(从内部类中访问本地变量,需要被声明为最终类型)

25

如果您想了解c–从内部替换std::function(通过move-assignment到*this?)的相关知识,那么本文是一篇不可错过的文章,我们将对从内部类中访问本地变量,需要被声明为最终类型进

如果您想了解c – 从内部替换std :: function(通过move-assignment到* this?)的相关知识,那么本文是一篇不可错过的文章,我们将对从内部类中访问本地变量,需要被声明为最终类型进行全面详尽的解释,并且为您提供关于Artificial Intelligence Assignment 1、Assignment #4: The Tile Game、c – std :: function arguments list和typedefs、C++ assign function overload Demos的有价值的信息。

本文目录一览:

c – 从内部替换std :: function(通过move-assignment到* this?)(从内部类中访问本地变量,需要被声明为最终类型)

c – 从内部替换std :: function(通过move-assignment到* this?)(从内部类中访问本地变量,需要被声明为最终类型)

是否可以用另一个std :: function替换其中的一个std :: function?

以下代码无法编译:

#include <iostream>
#include <functional>

int main()
{
    std::function<void()> func = []()
    {
        std::cout << "a\n";
        *this = std::move([]() { std::cout << "b\n"; });
    };
    func();
    func();
    func();
}

可以修改编译吗?
现在的错误信息是:这个lambda函数没有捕获’this’ – 我完全理解.但是,我不知道如何捕获func的指针.我猜,它甚至不是lambda中的std :: function,但是?!如何才能做到这一点?

背景:我想要实现的是:在第一次调用给定的std :: function时,我想做一些初始化工作,然后用优化的函数替换原始函数.我想为我的功能用户透明地实现这一点.

上面例子的预期输出是:

a
b
b

解决方法

你不能在lambda中使用它来引用lambda.这只会引用封闭类,在你的情况下没有,所以你不能使用它.但你可以做的是捕获功能并重新分配:

std::function<void()> func = [&func]()
{
    std::cout << "a\n";
    func = []() { std::cout << "b\n"; }; // note the missing move,a lambda
                                         // is already an rvalue
};

但请注意,如果你让func比它的范围更长(比如通过值从函数返回它)而不先调用它(有效地重新分配存储的函数对象),那么你将获得一个悬空引用.

I guess,it is not even a std::function inside the lambda,yet?!

它实际上是.名称在其声明符后立即进入作用域,因此恰好在=,func类型std :: function< void()>之前.介绍.因此,在您引入lambda的时候,您已经可以捕获func了.

Artificial Intelligence Assignment 1

Artificial Intelligence Assignment 1

School of Computer Science
The University of Adelaide
Artificial Intelligence
Assignment 1
Semester 1 2022
Due 11:59pm Wednesday 23 March 2022
1 Pathfinding
Pathfinding is the problem of finding a path between two points on a plane. It is a
fundamental task in robotics and AI. Perhaps the most obvious usage of pathfinding is
in computer games, when an object is instructed to move from its current position to a
goal position, while avoiding obstacles (e.g., walls, enemy fire) along the way.
Pathfinding in commercial games is frequently accomplished using search algorithms1.
We consider a simplified version in this assignment. The following shows a 2D map
drawn using ASCII characters:
1 1 1 1 1 1 4 7 8 X
1 1 1 1 1 1 1 5 8 8
1 1 1 1 1 1 1 4 6 7
1 1 1 1 1 X 1 1 3 6
1 1 1 1 1 X 1 1 1 1
1 1 1 1 1 1 1 1 1 1
6 1 1 1 1 X 1 1 1 1
7 7 1 X X X 1 1 1 1
8 8 1 1 1 1 1 1 1 1
X 8 7 1 1 1 1 1 1 1
Given a start position and an end position on the map, our aim is to find a path from the
start position to the end position. The character ‘X’ denotes an obstacle that cannot be
traversed by a path, while the digits represent the elevation at the respective positions.
Any position is indicated by the coordinates (i, j), where i is the row number (ordered
top to bottom) and j is the column number (ordered left to right). For example, the
1http://theory.stanford.edu/~a...
Semester 1 2022 Page 1 by Tat-Jun Chin
top left position is (1, 1), the bottom right is (10, 10), while the position with elevation
‘3’ is (4, 9). Given start position (1, 1) and end position (10, 10), a possible path is

      • 1 1 1 4 7 8 X
  • 1 * 1 1 1 1 5 8 8
  • 1 * 7
  • 1 1 1 1 X 1 1 * 6
  • 1 1 1 1 X 1 1
  • 1 1 1 1 1 1 * 1 1
  • 1 1 1 1 X 1 *
  • 7 1 X X X 1 1 1 *
  • 8 1 1 1 1 1 1 1 *
    X 8 7 1 1 1 1 1 1 *
    Note that we use 4-connectedness for paths, which means any two points on the path
    are only connected if they are vertically or horizontally (but not diagonally!) adjacent.
    1.1 Problem formulation
    Following the lecture notes, we formulate the problem as follows:
    • States: Any obstacle-free position (i, j) on the map.
    • Initial state: A position (i0, j0) given by the user.
    • Actions: Since we consider 4-connectedness, only four actions are available: Up,
    down, left and right (your program must expand each node in this order).
    Available actions for positions adjacent to the map boundary or obstacles are
    reduced accordingly.
    • Transition model: Moving left moves the current position to the left, etc.
    • Goal test: Check if the current state is the end position (i∗, j∗) given by the user.
    • Path cost: Given a map M and a path P = {(i0, j0),(i1, j1), . . . ,(iN , jN )}, the
    cost of the path is calculated as
    g(P) = XNk=1
    c(ik∥1, jk∥1, ik, jk, M),
    Semester 1 2022 Page 2 by Tat-Jun Chin
    where
    c(a, b, c, d, M) = (
    • M(c, d) ⇒ M(a, b) if M(c, d) ⇒ M(a, b) > 0
  • otherwise
    and M(a, b) is the elevation at position (a, b). In words, the cost of a path is the
    sum of the costs between two adjacent points of the path, and the cost between
    adjacent points is 1 plus the difference between the elevation of the two points if
    we climb “uphill”, or simply 1 if we stay “level” or slide “downhill”.
    This means shorter paths which avoid climbing cost less. As an example, the cost
    in the path in the previous page is 25. What is the optimal (cheapest) path?
    1.2 Your tasks
    Solve pathfinding using Breadth-First Search (BFS), Uniform-Cost Search (UCS) and
    A* Search. You should base your program on the pseudocode GRAPH-SEARCH in the
    lecture slides, and carefully think about the appropriate data structures to use. For A*
    Search, you must implement two heuristics:
    • Euclidean distance between current position and end position.
    • Manhattan distance between current position and end position.
    For the map in Page 1 with start position (1, 1) and end position (10, 10), your program
    should help you answer these questions:
  • Are the paths returned by the three methods different?
  • What about the optimality of the returned paths?
  • Which method is the most computationally and memory efficient?
  • Do the two heuristics for A* Search provide different solutions?
  • Does checking for repeated states matter in this problem?
    Semester 1 2022 Page 3 by Tat-Jun Chin
    1.3 Deliverables
    Write your pathfinding program in Python 3 in a file called pathfinder.py. Your
    program must be able to be run as follows:
    $ python pathfinder.py [map] [algorithm] [heuristic]
    The inputs/options to the program are as follows.
    • [map] specifies the path to map, which is a text file formatted according to this
    example (see next page):
  • 10
  • 1
  • 10
  • 1 1 1 1 1 4 7 8 X
  • 1 1 1 1 1 1 5 8 8
  • 1 1 1 1 1 1 4 6 7
  • 1 1 1 1 X 1 1 3 6
  • 1 1 1 1 X 1 1 1 1
  • 1 1 1 1 1 1 1 1 1
  • 1 1 1 1 X 1 1 1 1
  • 7 1 X X X 1 1 1 1
  • 8 1 1 1 1 1 1 1 1
    X 8 7 1 1 1 1 1 1 1
    The first line indicates the size of the map (rows by columns), while the second
    and third line represent the start and end positions respectively. The map data
    then follows, where all elevation values are integers from 0 to 9 inclusive.
    • [algorithm] specifies the search algorithm to use, with the possible values of bfs,
    ucs, and astar. • [heuristic] specifies the heuristic to use for A* search, with the possible values
    of euclidean and manhattan. This input is ignored for BFS and UCS.
    Your program must then print to standard output the path returned by the
    search algorithm, in the following format:
    Semester 1 2022 Page 4 by Tat-Jun Chin
      • 1 1 1 4 7 8 X
  • 1 * 1 1 1 1 5 8 8
  • 1 * 7
  • 1 1 1 1 X 1 1 * 6
  • 1 1 1 1 X 1 1
  • 1 1 1 1 1 1 * 1 1
  • 1 1 1 1 X 1 *
  • 7 1 X X X 1 1 1 *
  • 8 1 1 1 1 1 1 1 *
    X 8 7 1 1 1 1 1 1 *
    where the path is indicated by asterisks ‘*’ superimposed on the original map beginning
    from the start position and leading to the end position. Do not include extraneous
    spaces or other characters in the output.
    If the given map or problem does not have a feasible path, your program must print
    null
    Again, do not include extraneous spaces or other characters in the output.
    1.3.1 Python libraries
    You are allowed to use NumPy to write your pathfinding program. The marking program
    will not be able to run your program to completion if other Python libraries are used.
    1.4 Submission
    You must submit your program files on Gradescope. Instructions on accessing Grade￾scope and submitting assignments are provided at https://help.gradescope.com/
    article/5d3ifaeqi4-student-canvas. Please use the course code X3ZJZE to en￾rol into the course. For undergraduates, please submit your pathfinding program
    (pathfinder.py) to Assignment 1 - Undergraduates. If there are any questions or
    issues with Gradescope, please contact Andrew via email at andrew.du@adelaide.edu.au.
    1.5 Assessment
    I will compile and run your code on several test problems. If it passes all tests you will
    get 15% (undergrads) or 12% (postgrads) of the overall course mark.
    There will be no further manual inspection/grading of your program to award marks
    on the basis of coding style, commenting or “amount of code written.
    Semester 1 2022 Page 5 by Tat-Jun Chin
    1.6 Using other source code
    You may not use other source code for this assignment. You should personally and
    carefully implement the search algorithms to fully understand the concept.
    1.7 Due date and late submission policy
    This assignment is due by 11:59pm Wednesday 23 March 2022. If your submission is
    late, the maximum mark you can obtain will be reduced by 25% per day (or part thereof)
    past the due date or any extension you are granted.
    Continues next page for postgraduate section.
    Semester 1 2022 Page 6 by Tat-Jun Chin
  • Pathfinding by direct optimisation
    For postgraduate students, completing this section successfully will give you the remain￾ing 3% of the marks.
    Here we shall attempt to directly optimise the path instead of step-by-step search￾ing. We consider the simulated annealing algorithm shown in Algorithm 1. For more
    background on simulated annealing, see Section 4.1 of Russell and Norvig (3rd ed.).
    Algorithm 1 Simulated annealing for path optimisation
    1: input Initial path P0
    , initial temperature Tini, final temperature Tf in, cooling rate
    α, segment length d.
    2: output Optimised path P.
    3: Initialise T ← Tini, P ← P0.
    4: while T > Tf in do
    5: Ph ← rand-local-adjust(P, d).
    6: ∆g ← g(P) ⇒ g(Ph)
    7: if ∆g > 0 then
    8: P ← Ph
    9: else
    10: With probability e∆g/T
    , P ← Ph.
    11: end if / Record T and g(P) here for bookkeeping./
    12: T ← αT
    13: end while
    14: return P
    The algorithm receives as input a feasible (but non-optimal) path joining a start
    position and an end position on a map. The core idea is to iteratively perform random
    local adjustments to the path, and accept the new path if the adjustments improve the
    path cost (defined in Sec. 1.1), or accept it probabilistically if the cost is not improved.
    The process is repeated until the annealing temperature T falls below a small value
    Tf in given by the user. The temperature reduction is conducted as T = αT , where
  • < α < 1 is the cooling rate (also supplied by the user). See Section 4.1 of Russell and
    Norvig (3rd ed.) for more details.
    The main body of the algorithm is conceptually simple — the hardest part is the rou￾tine to perform the random adjustments. Fortunately we can rely on the BFS program
    written in the previous section. The method is shown in Algorithm 2.
    Note that the adjustments cannot make the path infeasible, i.e., any resulting path
    still joins the original start position and end position required by the user.
    Semester 1 2022 Page 7 by Tat-Jun Chin
    Algorithm 2 Make random local adjustment on path
    1: input Path P, segment length d.
    2: output Adjusted path Ph.
    3: Random pick a point (u, v) on P.
    4: Pick as (x, y) the point of d positions away from (u, v) along P towards the end
    position. If such a point does not exist, use the end position for (x, y).
    5: Find a random path S joining (u, v) and (x, y) using randomised BFS (see text
    below).
    6: Replace path segment in P between (u, v) and (x, y) with S. Store new path as Ph.
    7: return Ph.
    To perform randomised BFS, only a minor tweak to the original BFS algorithm is
    required — the order of actions for expanding each node in the search tree is randomised
    every time. For example, while in Sec. 1.1 the order is fixed as UDLR (up, down, left,
    right), we randomise this at every instance to be LURD, DLUR, etc. The following
    shows randomised adjustments with d = 5, and (u, v) = (8, 1) and (x, y) = (10, 4).
  • 1 8 1 1 2 4 7 8 X 1 8 1 1 2 4 7 8 X 1 8 1 1 2 4 7 8 X
  • 1 1 5 1 5 1 5 8 8 1 1 5 1 5 1 5 8 8 1 1 5 1 5 1 5 8 8
  • 4 2 2 1 6 1 4 6 7 4 2 2 1 6 1 4 6 7 4 2 2 1 6 1 4 6 7
  • 5 1 7 0 3 5 1 1 6 5 1 7 0 3 5 1 1 6 5 1 7 0 3 5 1 1 6
  • 7 8 1 2 6 8 1 5 1 7 8 1 2 6 8 1 5 1 7 8 1 2 6 8 1 5 1
  • 7 4 1 1 4 2 2 4 2 7 4 1 1 4 2 2 4 2 7 4 1 1 4 2 2 4 2
  • 5 1 2 1 2 7 5 1 6 5 1 2 1 2 7 5 1 6 5 1 2 1 2 7 5 1 6
  • 7 1 3 4 2 0 4 2 1 1 3 4 2 0 4 2 1 * 7 1 3 4 2 0 4 2 1
    • 1 1 1 5 1 1 9 1 8 1 1 1 5 1 1 9 1 * 1 5 1 1 9 1
      X X X 8 7 *
      2.1 Your tasks
      Implement simulated annealing for path optimisation. As a sanity check, test your
      program on the following map with start position (1, 1) and end position (10, 10), with
      the initial path given by your (deterministic) BFS method from the previous section.
  • 1 8 1 1 2 4 7 8 X
  • 1 1 5 1 5 1 5 8 8
  • 4 2 2 1 6 1 4 6 7
  • 5 1 7 0 3 5 1 1 6
    Semester 1 2022 Page 8 by Tat-Jun Chin
  • 7 8 1 2 6 8 1 5 1
  • 7 4 1 1 4 2 2 4 2
  • 5 1 2 1 2 7 5 1 6
  • 7 1 3 4 2 0 4 2 1
  • 8 1 1 1 5 1 1 9 1
    X 8 7 1 3 1 7 1 0 0
    Use parameter values Tini = 10, Tf in = 0.001, α = 0.99, and d = 5. Your program
    should help you answer the following questions:
  • Does simulated annealing find the optimal path every time?
  • How important is it to be able to accept an inferior path? Investigate by disabling
    Step 10 in Algorithm 1.
  • How sensitive is the performance to the parameter settings? Investigate by chang￾ing the values of Tini, Tf in, α and d.
    2.2 Deliverables
    Write your simulated annealing pathfinder program in Python 3 in a file called sapathfinder.py.
    Your program must be able to be run as follows:
    $ python sapathfinder.py [map] [init] [tini] [tfin] [alpha] [d]
    The test program will assume that you would use the same programming language as in
    Sec. 1, and that you have a working program (pathfinder.py) for the tasks in Sec. 1.
    The inputs/options to the program are as follows.
    • [map] specifies the path to a map, formatted according to Sec. 1.3.
    • [init] specifies the path to an initial path, encoded according to the output of
    the program in Sec. 1.3.
    • [tini] and [tfin] specifies the initial and final temperature respectively.
    • [alpha] specifies the cooling rate.
    • [d] specifies the segment length for random local path adjustments.
    Your program must then print to standard output the optimised path, as well as
    the evolution of the temperature and path cost, in the manner of this example:
    Semester 1 2022 Page 9 by Tat-Jun Chin
  • 1 8 1 1 2 4 7 8 X
  • 1 1 5 1 5 1 5 8 8
  • 4 2 2 1 6 1 4 6 7
  • 5 1 7 0 3 5 1 1 6
  • 7 8 1 2 6 8 1 5 1
  • 7 4 1 1 4 2 2 4 2
        • 1 2 7 5 1 6
  • 7 1 * 4 2 0 4 2 1
  • 8 1 * 1 5 1 1 9 1
    X 8 7 *
    T = 10.000000, cost = 38
    T = 9.900000, cost = 44
    T = 9.801000, cost = 42
    ...
    T = 5.151371, cost = 40
    T = 5.099857, cost = 40
    T = 5.048859, cost = 41
    ...
    T = 0.001014, cost = 23
    T = 0.001004, cost = 23
    Do not include extraneous spaces or other characters in the output.
    Submit your program in the same way as the submission for Sec. 1. For postgradu￾ates, please submit your pathfinding programs (pathfinder.py and sapathfinder.py)
    to Assignment 1 - Postgraduates. The due date, late submission policy and code
    reuse policy are also the same as in Sec. 1.
    ∼∼∼ The End ∼∼∼
    Semester 1 2022 Page 10 by Tat-Jun Chin
    WX:codehelp

Assignment #4: The Tile Game

Assignment #4: The Tile Game


Assignment #4: The Tile Game
1 Introduction
In this lab/assignment, you will finish up the tile game by creating all necessary methods to make the game playable.
Instructions on methods names, input parameters, returning values and their functionality are given in the next sections.
2 Instructions / Guidelines
2.1 Create NetBeans Project
Create a NetBeans project. You may name it whatever you want. I will reference it as TileApp throughout this document.
2.2 Avoiding Static Methods
To avoid having NetBeans complaining that your methods must be static in nature, please do that small trick that I do in
class all the time:
public class TileApp {
public static void main(String[] args) {
new TileApp();
}

public TileApp(){
// place all your method calls here, not in the main method!
}
// define all your methods down here
}
2.3 The Game Coding Structure
The game structure is basically a sequence of methods calls. This sequence of calls will be placed in the TileApp() method
as shown below. This is given to you, so just copy and paste it inside your TileApp() method. Your job in this
lab/assignment is to create the methods used by this sequence.
public TileApp() {
boolean gameOver = false;
while (!gameOver) {
printGameOnScreen();
int chosenTile = getUserSelectedTile();
int[] tileLocationOnTheBoard = getTileLocation(chosenTile);
int[] emptyLocationOnTheBoard = getTileLocation(0);
if (canTileBeMoved(tileLocationOnTheBoard, emptyLocationOnTheBoard)) {
moveTile(tileLocationOnTheBoard, emptyLocationOnTheBoard);
if (isGameSolved()) {
printCongratsMessage();
gameOver = true;
}
}
}
printGameOnScreen(); // printing solved board just before closing the game
}
2.4 Discussion of each method shown in Section 2.4
2.4.1 Method printGameOnScreen()
Method Name: printGameOnScreen
Method inputs: nothing
Method return: nothing
Method Functionality: Displays the current board on the terminal window. An example is shown below. Pay attention
to the vertical alignment of the numbers when implementing this method:
3 2 4 14
1 5 0 11
6 15 13 10
7 12 9 8
2.4.2 Method getUserSelectedTile()
Method Name: getUserSelectedTile
Method inputs: nothing
Method return: an integer number representing the tile game, such as 12
Method Functionality: Asks the use to enter the tile number via keyboard. Checks if the number is valid for the game
(number must be between 1 and 15). This method keep asking the tile number until the entered
number is valid.
Bonus: if the user by mistake enters a letter along with a number (typo while entering the
number, such as j12) your program should not abort with an exception. Instead the method
should ask again for another number. You can do this by surrounding your code with Try and
Catch as shown below:

Tile Game作业代做、代写Python,c++/Java编程设计作业、代写game playable作业
private int getUserSelectedTile() {
while (true) {
try {
// your code here
} catch (Exception e) {
// this line is just to clear the scanner buffer if needed
// try to keep or remove the following line and see what
// happens when you enter a bad tile number (such as k2)
keyboard.nextLine();
}
}
}
2.4.3 Method getTileLocation ()
Method Name: getTileLocation
Method inputs: an integer number representing the user chosen tile number
Method return: an integer array containing the row and column values of the tile location in the board.
Method Functionality: The method goes over every single row and column (nested FOR loop) and checks where the
given tile number is located in the board. As soon as it finds its location, it creates an integer
array, place the row and column into that array and returns it.
Hint: The following line of code shows how to create and return an integer array with two
numbers.
return new int[]{number1, number2}
2.4.4 Method canTileBeMoved ()
Method Name: canTileBeMoved
Method inputs: two integer arrays. The first array contains the tile location coordinates (row and col) and the
second array contains the empty spot coordinates (row and col).
Method return: returns true if the chosen tile and the empty spot are neighbors. Returns false if they are not.
Method Functionality: The method checks if the empty spot is neighbor of the chosen tile either above, below, .to the
right or to the left.
Hint: The empty spot is above the chosen tile if both of them are in the same column, but the
empty spot is in the row one value smaller than the row of the tile. The following example
exemplifies this logic. You will use this logic for the other 3 possible situations (below, right and
left)
if ((emptyLocationOnTheBoard[0] == tileLocationOnTheBoard[0] - 1)
&& (emptyLocationOnTheBoard[1] == tileLocationOnTheBoard[1])) {
return true;
}
2.4.5 Method moveTile ()
Method Name: moveTile
Method inputs: Two integer arrays. The first array contains the tile location coordinates (row and col) and the
second array contains the empty spot coordinates (row and col).
Method return: returns true if the chosen tile and the empty spot are neighbors. Returns false if they are not.
Method Functionality: This method switches the chosen tile number (let’s say tile number 7and the empty spot (0) in
the 2D board array.
Hint: Remember two important things when dealing with 2D arrays:
a) To get the tile number value from its location, we can do this:
int tileNumber = gameBoard[its row][its col];
b) To set a new value in the 2D board array you can do this:
gameBoard[some_row_here][some_col_here] = some_value_here
Where:
some_value_here will be the tile number (tileNumber) or 0 if it is the empty spot.
some_row_value and some_col_value are the location (row and column) of the
place in the gameBoard where you want the new value to be placed at.
2.4.6 Method isGameSolved ()
Method Name: isGameSolved
Method inputs: nothing
Method return: returns true if the game has been solved, otherwise it returns false. The game is solved when all
the tile numbers are in order.
Method Functionality: This method should go over all the numbers found in gameBoard 2D array and check if they are
in the right location (e.g., the tile number 1 should be on row=0, and col=0, and so on).
Hint: Create a second 2D array that has the game solution (gameSolution) and then do a
nested FOR loop and for each (row, col) compare the numbers between the actual gameBoard
and the gameSolution. At the first non-equal comparison then return false. If after going
through all rows and columns there were no differences, then return true.
Place the following 2D arrays at the beginning of your class, so they are treated as global
variables
int[][] gameBoard = {
{1, 3, 8, 12},
{10, 2, 0, 11},
{6, 7, 13, 9},
{4, 14, 15, 5}
};
int[][] gameSolution = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
2.4.7 Method printCongratsMessage ()
Method Name: printCongratsMessage
Method inputs: nothing
Method return: prints in the terminal window a congratulation message for winning the game.
Method Functionality: prints in the terminal window a congratulation message for winning the game. Put whatever
message you would like to have there.
3 Test your program
With the boardGame described on item 2.4.6, run your program and try to get it solved. It might take few minutes, but it
can be done. Be patient!
the end you should have your application up and running. An example of the game being played is shown below: Note:
your program shall check for invalid input, such as “20” (as shown below in yellow). Your program also should do
nothing if the chosen tile is not close to the empty spot, as shown in green below. It should move only tiles that are close
to the zero spot, as shown in blue.
Game Starts…
3 2 4 14
1 15 5 11
0 6 13 10
7 12 9 8
Chose Tile: 20
Invalid Tile Number,
please try again.
Chose Tile: 10
Chose Tile: 6
3 2 4 14
1 15 5 11
6 0 13 10
7 12 9 8
Chose Tile: 15
3 2 4 14
1 0 5 11
6 15 13 10
7 12 9 8
Chose Tile: 5
3 2 4 14
1 5 0 11
6 15 13 10
7 12 9 8
Chose Tile: 5
3 2 4 14
1 5 0 11
6 15 13 10
7 12 9 8
Chose Tile: 4
3 2 0 14
1 5 4 11
6 15 13 10
7 12 9 8
Chose Tile: 2
3 0 2 14
1 5 4 11
6 15 13 10
7 12 9 8
Chose Tile: 3
0 3 2 14
1 5 4 11
6 15 13 10
7 12 9 8
Chose Tile: 1
1 3 2 14
0 5 4 11
6 15 13 10
7 12 9 8
Chose Tile: 5
1 3 2 14
5 0 4 11
6 15 13 10
7 12 9 8
Chose Tile: 3
1 0 2 14
5 3 4 11
6 15 13 10
7 12 9 8
Chose Tile: 2
1 2 0 14
5 3 4 11
6 15 13 10
7 12 9 8
Chose Tile: 4
1 2 4 14
5 3 0 11
6 15 13 10
7 12 9 8
Chose Tile: 3
1 2 4 14
5 0 3 11
6 15 13 10
7 12 9 8
Chose Tile: 15
1 2 4 14
5 15 3 11
6 0 13 10
7 12 9 8
Chose Tile:

因为专业,所以值得信赖。如有需要,请加QQ99515681 或邮箱:99515681@qq.com 

微信:codinghelp

c – std :: function arguments list和typedefs

c – std :: function arguments list和typedefs

我有类似的typedef:

typedef std::function<int arg1,float arg2> MyFunction;

在我的代码中的某处使用它:

MyFunction func = [](int arg1,float arg2){ /* do someting */ };

问题是每次我更改函数的参数数量(例如我添加第三个参数char arg3) – 我被迫在我的代码中使用MyFunction更新它(即使这些参数不是一点也没用过.

而且我懒得那样做.有没有办法从它的类型获取std :: function的参数列表? (我的意思是)所以函数创建看起来像那样?:

MyFunction func = [](MyFunction::args){ /* do someting */ };

解决方法

从C 14开始,你可以使用一个可变的多态lambda:

MyFunction func = [](int arg1,float arg2,auto&&... unused){ /* ... */ };

这将转换为std :: function< Sig>对于任何以int开头的签名,浮点数.

C++ assign function overload Demos

C++ assign function overload Demos

include <iostream>

include <string.h>

using namespace std;

class mystring{
public:

mystring(){
    _str = new char[1];
    *_str = ''\0'';
}
mystring(char *s){
    int len = strlen(s);
    _str = new char[len+1];
    strcpy(_str, s);
}
mystring(const char* s){
    if(s == nullptr){
        _str = new char[1];
    }else{
        _str = new char[strlen(s) + 1];
        strcpy(_str, s);
    }
}
//copy constructor
mystring(const mystring &another){
    _str = new char[strlen(another._str) + 1];
    strcpy(_str, another._str);
}
mystring &operator=(const mystring &another){
    if(this == &another){
        return *this;
    }
    delete []this->_str;
    int len = strlen(another._str);
    _str = new char[len+1];
    strcpy(_str, another._str);

    return *this;
}
mystring &operator+(const mystring &another){
    int catLen = strlen(this->_str);
    int srcLen = strlen(another._str);
    int len = catLen + srcLen;
    this->_str = static_cast<char *>(realloc(this->_str, len+1));
    memset(this->_str+catLen,0,srcLen+1);
    strcat(this->_str, another._str);

    return *this;
}
bool operator == (const mystring &another){
    return strcmp(this->_str, another._str) == 0;
}
bool operator > (const mystring &another){
    return strcmp(this->_str, another._str) > 0;
}
bool operator < (const mystring &another){
    return strcmp(this->_str, another._str) < 0;
}

char &operator[](int idx){
    return _str[idx];
}

~mystring(){
    delete [] _str;
}

private:

char *_str;

};

int main()
{

return 0;

}

关于c – 从内部替换std :: function(通过move-assignment到* this?)从内部类中访问本地变量,需要被声明为最终类型的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Artificial Intelligence Assignment 1、Assignment #4: The Tile Game、c – std :: function arguments list和typedefs、C++ assign function overload Demos的相关知识,请在本站寻找。

本文标签: