小编nik*_*hil的帖子

在C++中迭代std :: set <std :: string>时出现分段错误

我的代码的这部分(对于这个项目)给了我一个分段错误.源代码可在此处获得.

void PackageManager::install_package(string pname)
{
  if(repository->exists_package(pname)) {
    Package *pkg;
    ConcretePackage *cpkg;
    MetaPackage *mpkg;
    if(repository->is_virtual(pname)) {
      //code for dealing with meta packages
      mpkg = new MetaPackage(pname);
      pkg = mpkg;
      system->operator+(pname);
    } else {
      //code for dealing with concrete packages
      cpkg = new ConcretePackage(pname);
      pkg = cpkg;
      system->operator+(pname);
      if( cpkg->getDependencies().size() > 0) {
        for(set<string>::iterator sit = pkg->getDependencies().begin();
            sit!=pkg->getDependencies().end(); ++sit) {
          cout<<*sit<<endl;
          system->operator+(*sit);
        }
      }
    }
  } else {
    cout<<"Invalid Package Name"<<endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我运行gdb和回溯时的错误.

Program received signal SIGSEGV, …
Run Code Online (Sandbox Code Playgroud)

c++ iterator set segmentation-fault

1
推荐指数
1
解决办法
3046
查看次数

将 double 值截断为 6 位小数

我正在解决一个需要截断输出的问题,令我惊讶的是,我无法找到一种在 java 中截断数字的方法。

输出需要是后跟 6 位小数的数字。

我想要的是 double truncate(double number,int places)输出是 truncate(14/3.0) = 4.666666.

但我得到的是

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
  }
// rounds( 14/3.0 , 6 ) = 4.666667
Run Code Online (Sandbox Code Playgroud)

随着String.format我得到

String.format("%.6f", 14/3.0) = 4.666667
Run Code Online (Sandbox Code Playgroud)

我还尝试了我在 stackoverflow 上找到的一个解决方案,该解决方案建议使用 BigDecimal 并且给了我相同的答案。

NumberFormat 似乎也以同样的方式工作

java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(6); …
Run Code Online (Sandbox Code Playgroud)

java math truncate

1
推荐指数
1
解决办法
8888
查看次数

ORA-00907即使括号平衡,也缺少右括号

我有一个相对简单的SQL查询拒绝在sqldeveloper上执行,我已经缩小了对此的违规行 -

and (b.date_updated > (sysdate MINUS 2) or a.date_updated > (sysdate MINUS 2))
Run Code Online (Sandbox Code Playgroud)

这是我的where条款的一部分以及其他陈述.

我在视觉上多次对括号进行了计算,它看起来与我平衡,我不确定我在这里做错了什么,有人可以帮我弄清楚这里有什么问题.

为了完整性,这是where子句的样子

where a.customer_id = b.customer_id
and (b.date_updated > (sysdate MINUS 2) or a.date_updated > (sysdate MINUS 2))
and a.c_id = c.c_id 
Run Code Online (Sandbox Code Playgroud)

sql oracle syntax-error

1
推荐指数
1
解决办法
206
查看次数

CLLocation可能无法响应setDistanceFilter

我是iPhone的编程新手,我跟着本书.我坚持第4章,授权和核心位置的例子.

这是我到目前为止编写的代码:WhereamiAppdelegate.h

    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>

    @interface WhereamiAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate> {
        UIWindow *window;
        CLLocation *locationManager;

    }

    @property (nonatomic, retain) IBOutlet UIWindow *window;

    @end
Run Code Online (Sandbox Code Playgroud)

这是实现文件:我只包含了我所做的更改.整个文件都在这里.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Create location manager.
        locationManager = [[CLLocation alloc] init];
        [locationManager setDelegate:self];
        [locationManager setDistanceFilter:kCLDistanceFilterNone];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [locationManager startUpdatingLocation];

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [locationManager setDelegate:nil];
    [_window release];
    [super dealloc];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@",newLocation);
}

- (void)locationManager:(CLLocationManager …
Run Code Online (Sandbox Code Playgroud)

iphone core-location ios

0
推荐指数
1
解决办法
1006
查看次数

如何在c ++集中插入值

我正在尝试创建Point类的一组自定义对象

point.h

#ifndef POINT_H
#define POINT_H

class Point
{
      float x_coordinate,y_coordinate;
      public:
             Point(float x, float y):x_coordinate(x),y_coordinate(y)
             {}

             float get_x()
             {
                   return x_coordinate;
             }

             float get_y()
             {
                   return y_coordinate;
             }

             bool operator==(Point rhs)
             {
                  if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
                      return true;
                  else return false;
             }

             bool operator<(Point rhs)
             {
                  if((int)x_coordinate < (int)rhs.get_x())
                      return true;
                  else return false;
             }
};

#endif
Run Code Online (Sandbox Code Playgroud)

我刚开始写驱动程序

#include<iostream>
#include<set>
#include "point.h"
using namespace std;



int main()
{
    Point p1(-10,-10),p2(-10,10),p3(10,10),p4(10,-10);
    set<Point> points_set = set<Point>(); …
Run Code Online (Sandbox Code Playgroud)

c++ stl set

0
推荐指数
1
解决办法
1万
查看次数

解析文本文件时抛出std :: out_of_range

我有以下代码来读取文本文件.

const string FILENAME = PACKAGES_DIR + pname;
  //the arguments to ifstream is a cstring and hence the conversion must be made
  ifstream freader;
  freader.open(FILENAME.c_str(),ios::in);
  if(freader.is_open())
  {
    while(freader.good())
    {
      string line;
      getline(freader,line);
      cout<<line<<endl;
      if(line.find("PackageId:"))
      {
        cout<<line.substr(11)<<endl;
      }
      else if(line.find("Name:"))
      {
        cout<<line.substr(5)<<endl;
      }
      else if(line.find("Version:"))
      {
        cout<<line.find(8)<<endl;
      }
      else
      {
        cout<<line<<endl;
      }

    }
  }
Run Code Online (Sandbox Code Playgroud)

有问题的文本文件的内容是

PackageId:994
Name:basket
Version:1.80-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

PackageId:994
geId:994
Name:basket

Version:1.80-1
0-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
e-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

PackageId:994
994
Name:basket …
Run Code Online (Sandbox Code Playgroud)

c++ file-io text

0
推荐指数
1
解决办法
1082
查看次数

提交前验证表格(输入所有字段)

我正在尝试检查下面表单中的字段是否已填充,然后才能将其插入数据库,例如显示弹出的字段尚未填写.这只是一个简单的注册表单.

<form name="form1" method="post" action="signup_ac.php">
<strong>Sign up</strong>
Username:<input name="username" type="text" id="username" size="30">
Password:<input name="password" type="password" id="password" size="15">
Name:<input name="name" type="text" id="name" size="30">
<select name="Month">
<option selected>Month</option>
<option value="January">January</option>
<option value="Febuary">Febuary</option
  </select> 
<select name=Year>
<option selected>Year</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
 </select>
<input type="submit" name="Submit" value="Submit"> &nbsp;
<input type="reset" name="Reset" value="Reset">
</form>
Run Code Online (Sandbox Code Playgroud)

我如何使用JavaScript或jQuery执行此操作.

html javascript php jquery

0
推荐指数
1
解决办法
7598
查看次数

无法在列表中使用push_back元素

我的代码面临着一个非常奇怪的问题,我无法将元素推入列表中.

我正在尝试实现扫描填充算法.我想要在屏幕上绘制的点列表,以便我可以检查扫描线是否与它们相交.在我的Screen :: plot_pixel函数中,我将Point推入mapped_points列表.但是当我遍历列表时,它是空的.(我正在使用友元函数迭代shape.cpp)

我尝试使用套装但无济于事.我已经附加了控制台输出我也得到了.

plot_pixel被多次调用,我通过在那里添加一个print语句验证了这一点但是这些点拒绝被推入.这是我的所有代码,point.h

#ifndef POINT_H
#define POINT_H
class Point
{
    float x_coordinate,y_coordinate;
public:
    Point(){}
    Point(float x, float y):x_coordinate(x),y_coordinate(y){}
    float get_x() const{return x_coordinate;}
    float get_y() const {return y_coordinate;}

    bool operator==(const Point rhs)const
    {
        if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
            return true;
        else return false;
    }

    bool operator<(const Point rhs)const
    {
        if((int)x_coordinate < (int)rhs.get_x())
            return true;
        else return false;
    }
};
#endif
Run Code Online (Sandbox Code Playgroud)

screen.h

#ifndef SCREEN_H
#define SCREEN_H
#include<graphics.h>
#include "point.h"
#include<list>

class Shape;

class Screen
{ …
Run Code Online (Sandbox Code Playgroud)

c++ list

-4
推荐指数
1
解决办法
997
查看次数