2016-01-05 27 views
0

我定義C++ - 預期前一次表達式 '(' 令牌和丟失之前模板參數 '(' 令牌

typedef std::map< int, std::set<int> > SparseMap; 

然後我嘗試插入以這種方式的一對:

pair<SparseMap::iterator,bool> result; 
result = sparseBlue.insert(SparseMap::value_type(row, set(col))); //errors 
    if(result.second) 
     cout << "Inserted" << endl; 
  • col是整數矩陣座標
  • sparseBlue聲明as SparseMap sparseBlue;

爲什麼我在我插入的行處出現這些錯誤?

+1

當你使用'標準::對的結果會怎樣? – NathanOliver

+3

什麼是「行」?什麼是「col」?什麼是'sparseBlue'? – juanchopanza

+2

錯誤的;也在第三行。 –

回答

1

另一種解決方案是,你可以插入到地圖添加內容之前:

#include <map> 
#include <set> 

using namespace std; 

int main() 
{ 
    int row = 0; 
    int col = 0; 
    std::map<int, set<int>> sparseBlue; 

    // insert empty item 
    auto iter = sparseBlue.insert(std::make_pair(row, std::set<int>())); 

    // did a new item get inserted? 
    cout << "The item did " << (iter.second?"":"not") << " get inserted\n"; 

    // add item to set 
    (*iter.first). // the map iterator 
      second. // the set 
      insert(col); // what we want to do 
} 

std::map::insert返回值返回std::pair,表示該迭代器插入的項目,並根據新的項目是否插入truefalse。 `的`,而不是對 的結果;`

Live Example

+0

非常感謝您提供詳盡的示例!現在它更清晰。 – rh0x

2

我相信@ T.C和@Lightness Races在Orbit有正確的想法,需要std::set<int>。唯一的問題是std::set<T>沒有一個構造函數,該構造函數需要一個類型爲T(int的單個項)的構造函數。

假設你真的需要一組在地圖中的值,那麼你可能會想是這樣的:

std::set<int> tmpSet; 
tmpSet.insert(col); 
result = sparseBlue.insert(std::make_pair(row, tmpSet)); 
+0

就是這樣!它也適用於'result = sparseBlue.insert(SparseMap :: value_type(row,tmpSet));' – rh0x

+0

在C++ 11中,set有一個構造函數,它接受一個初始化列表,所以不用單獨的步驟就可以。 –

+0

哦是啊哎呀。哈哈擁有41代表。 +1 –

相關問題