具有功能签名的Const关键字(Const keyword with function signature)
这是我的简单代码:
#include <iostream> using namespace std; class alloc { }; template <typename T, typename Alloc = alloc> class vector { public: void swap(vector<T,Alloc> &v) { cout << "swap()" << endl; } }; template <typename T, typename Alloc> void swap(const vector<T,Alloc> &v1,const vector<T,Alloc> &v2) { v1.swap(v2); } int main() { vector<int> x; vector<int> y; swap(x,y); return 0; }
代码片段运行没有问题,但我无法获得任何输出
然后我删除
const
关键字。void swap(vector<T,Alloc> &v1,vector<T,Alloc> &v2)
我得到输出
swap()
我读过“原因是参数的const只适用于函数内部的本身,因为它正在处理数据副本,这意味着函数签名无论如何都是相同的。”
所以我认为写或不写const之间没有区别。如果我坚持在这里写const,我如何修改代码以获得输出
swap()
Here is my simple code:
#include <iostream> using namespace std; class alloc { }; template <typename T, typename Alloc = alloc> class vector { public: void swap(vector<T,Alloc> &v) { cout << "swap()" << endl; } }; template <typename T, typename Alloc> void swap(const vector<T,Alloc> &v1,const vector<T,Alloc> &v2) { v1.swap(v2); } int main() { vector<int> x; vector<int> y; swap(x,y); return 0; }
The code snippet run without problem.But i can't get any outputs
Then I delete
const
keyword.void swap(vector<T,Alloc> &v1,vector<T,Alloc> &v2)
I get output
swap()
I have read "The reason is that const for the parameter only applies locally within the function, since it is working on a copy of the data. This means the function signature is really the same anyways."
So i thought there is no difference between write or not write const.if i insist to write const here,how do i modify the code to get the output
swap()
原文:https://stackoverflow.com/questions/36808478
最满意答案
我写了自定义方法
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address { double latitude = 0, longitude = 0; NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json sensor=false&address=%@", esc_addr]; NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL]; if (result) { NSScanner *scanner = [NSScanner scannerWithString:result]; if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) { [scanner scanDouble:&latitude]; if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) { [scanner scanDouble:&longitude]; } } } CLLocationCoordinate2D center; center.latitude = latitude; center.longitude = longitude; return center; }
OUTPUT:
我什么时候通过字符串然后自动指向GoogleMap中的所需位置。
I wrote the custom method
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address { double latitude = 0, longitude = 0; NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json sensor=false&address=%@", esc_addr]; NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL]; if (result) { NSScanner *scanner = [NSScanner scannerWithString:result]; if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) { [scanner scanDouble:&latitude]; if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) { [scanner scanDouble:&longitude]; } } } CLLocationCoordinate2D center; center.latitude = latitude; center.longitude = longitude; return center; }
OUTPUT:
When ever I passed string then automatically pointing the required location in GoogleMap.
相关问答
更多-
谷歌地图最后的好地图位置(Google Maps Last Good Map Location)[2024-03-05]
您应该使用onLocationChanged侦听器。 您可以按如下方式实现它: public class myActivity implements LocationListener { @Override public void onLocationChanged(Location loc) { // if (loc.getLatitide() or loc.getLongitude() is within bounds) // save locat ... -
以下是Objective-C中有关如何在更新用户位置时使Map居中的示例: - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1000, 1000); [self.mapView set ...
-
我写了自定义方法 - (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address { double latitude = 0, longitude = 0; NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; ...
-
您是否想要对街道或POI(兴趣点)进行地理编码? 在这种情况下, geopy是完美的: In [1]: from geopy import geocoders In [2]: g = geocoders.Google(GOOGLE_MAPS_API_KEY) In [3]: (place, point) = g.geocode('Eiffel Tower, Paris') Fetching http://maps.google.com/maps/geo?q=Eiffel+Tower%2C+Paris&o ...
-
我不知道这是否对你有所帮助,但基本上这是做到这一点的一种方式: 编写位置输入的表单,视图和模板; 阅读有关如何使用Javascript设置Google Maps实例的信息 ; 当用户提交表单时,您的视图会使用urllib2调用Google Maps地理编码Web服务 (例如,URL为: http : //maps.googleapis.com/maps/api/geocode/json? &sensor = true 处理生成的JSON,提取纬度和 ...
-
你可以将init()函数编写为init1()两次,并为第二个表创建一个单独的文件来获取位置并调用init1()中的dat文件并给出不同的引脚颜色。 并在任何需要的地方调用这两个函数。我认为这样可行。 You can write the init() function twice as init1() and make a separate file for 2nd table to fetch the locations and call dat file inside init1() and give d ...
-
Bing Map图钉宽度设置将图钉移离地图位置(Bing Map Pushpin width setting moves the pin away from the map location)[2023-01-03]
我对JS 不太熟悉,但你不能只是偏移引脚,所以它位于正确的位置? We can add anchor property to the Pushpin as follows anchor: new Microsoft.Maps.Point(12,36) "12,36 indicates the size of the pushpin icon used." var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(parseFl ... -
基于文本的游戏地图(Map for text based game)[2023-07-14]
实际问题是,对于每个y和每个x ,您为每个对象打印一个字符: for y in range(mapS[1]): numy += 1 for x in range(mapS[0]): numx += 1 for place in objects: if objects[place][0] == numx: if objects[place][0] == numy: l ... -
你写错了这些代码 此代码用于获取当前lat和long数据 longitude = location.getLongitude(); latitude = location.getLatitude(); 并将此数据设置为在地图上设置 mMap = googleMap; LatLng latLng = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(latLng).draggable(true)); mMap.moveCame ...
-
打开android searchView小部件时获取当前地图位置(get current map location when open android searchView widget)[2022-03-20]
如果您使用的是Google Map API V2,请使用此方法。 它会在地图上显示我的位置按钮。 googleMap.setMyLocationEnabled(true); 或者您可以使用当前的lat-long在地图上添加标记。 API文档 例 If you are using Google Map API V2 then use this method. It will display my location button on map. googleMap.setMyLocationEnabled(t ...