Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. std::map<> in C#

std::map<> in C#

Scheduled Pinned Locked Moved C#
csharpc++help
12 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R raju_shiva

    Hi sir, As i am new C#. I developed a code in VC++ where i am reading the data from combo box and storing it in map. As i include header file,i can access

    std::map<CString,CString>m_mapId;
    std::map<CString,CString>::iterator it=m_mapId.begin();

    The same code i want to use in C# Any help will be thankful Thanks Raj

    L Offline
    L Offline
    Lukasz Nowakowski
    wrote on last edited by
    #2

    Didn't use C++ much, but I think, you should try Dictionary class.

    1 Reply Last reply
    0
    • R raju_shiva

      Hi sir, As i am new C#. I developed a code in VC++ where i am reading the data from combo box and storing it in map. As i include header file,i can access

      std::map<CString,CString>m_mapId;
      std::map<CString,CString>::iterator it=m_mapId.begin();

      The same code i want to use in C# Any help will be thankful Thanks Raj

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #3

      The closest thing to this, is probably the SortedDictionary. To do this code, all you'd do is:

      SortedDictionary<string,string>_map;
      // ... some code to fill the dictionary:
      foreach (KeyValuePair<string,string> kvp in _map)
      {
      // Do something with the data.
      }

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      • R raju_shiva

        Hi sir, As i am new C#. I developed a code in VC++ where i am reading the data from combo box and storing it in map. As i include header file,i can access

        std::map<CString,CString>m_mapId;
        std::map<CString,CString>::iterator it=m_mapId.begin();

        The same code i want to use in C# Any help will be thankful Thanks Raj

        L Offline
        L Offline
        Laxman Auti
        wrote on last edited by
        #4

        raju_shiva wrote:

        The same code i want to use in C#

        Here is the C# code

        Dictionary<string, string> hashMap = new Dictionary<string, string>();
        IDictionaryEnumerator en = hashMap.GetEnumerator();
        while (en.MoveNext())
        {
        string key = (string)en.Key;
        string keyValue = (string)en.Value;
        // Do what you want to do with key/value.
        }

        Knock out 't' from can't, you can if you think you can.

        R 1 Reply Last reply
        0
        • R raju_shiva

          Hi sir, As i am new C#. I developed a code in VC++ where i am reading the data from combo box and storing it in map. As i include header file,i can access

          std::map<CString,CString>m_mapId;
          std::map<CString,CString>::iterator it=m_mapId.begin();

          The same code i want to use in C# Any help will be thankful Thanks Raj

          D Offline
          D Offline
          Dave Doknjas
          wrote on last edited by
          #5

          Dictionary m_mapId = new Dictionary();
          Dictionary.Enumerator it = m_mapId.GetEnumerator();

          However, it's unusual to deal with enumerators directly in C# - instead just use a 'foreach' loop, which enumerates for you. David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com -- Modified Wednesday, June 30, 2010 5:36 PM

          L 1 Reply Last reply
          0
          • D Dave Doknjas

            Dictionary m_mapId = new Dictionary();
            Dictionary.Enumerator it = m_mapId.GetEnumerator();

            However, it's unusual to deal with enumerators directly in C# - instead just use a 'foreach' loop, which enumerates for you. David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com -- Modified Wednesday, June 30, 2010 5:36 PM

            L Offline
            L Offline
            Laxman Auti
            wrote on last edited by
            #6

            David Anton wrote:

            However, it's unusual to deal with enumerators directly in C# - instead just use a 'foreach' loop, which enumerates for you.

            Any example that lets unusual deal of C# enumerators?

            Knock out 't' from can't, you can if you think you can. :cool:

            1 Reply Last reply
            0
            • L Laxman Auti

              raju_shiva wrote:

              The same code i want to use in C#

              Here is the C# code

              Dictionary<string, string> hashMap = new Dictionary<string, string>();
              IDictionaryEnumerator en = hashMap.GetEnumerator();
              while (en.MoveNext())
              {
              string key = (string)en.Key;
              string keyValue = (string)en.Value;
              // Do what you want to do with key/value.
              }

              Knock out 't' from can't, you can if you think you can.

              R Offline
              R Offline
              raju_shiva
              wrote on last edited by
              #7

              Hi sir, Thanks for your reply. I am trying to use as u suggested but getting more confused. Actually i have developed code in VC where i store some values in and store in combo after sorting accordingly. Here is my complete code wt i am doing

              /*During initilizing i am strores vaues in map*/
              CTest1View::OnInitialUpdate()
              {
              CFormView::OnInitialUpdate();
              ResizeParentToFit();

              m\_mapId\["Abc"\];
              m\_mapId\["aa"\];
              

              }
              /*any character is typed in edit box*/
              CTest1View::OnEditchangeCombo1()
              {

              CString strvalue;  	
              CString strTemp;	
              m\_ctrlCombo.GetWindowText(strvalue);	//Get the text from combo
              std::map<CString,CString>::iterator it=m\_mapId.begin();//I will begin using iterator begin
              m\_ctrlCombo.ResetContent();	
              for(; it!=m\_mapId.end();it++)  // I will loop till the map end
              {
              	strTemp = it->first;		//get the first value from map
              	if(strTemp.Find(strvalue)==0)    //find the character in string(which is in map)
              		m\_ctrlCombo.AddString(strTemp); // Add to combo
              
              }
              
              m\_ctrlCombo.SetWindowText(strvalue);	//Get the window text
                  m\_ctrlCombo.ShowDropDown();	//Show the drop down
              

              }

              I am trying with C#

              Dictionary<string, int> d = new Dictionary<string, int>();

                      d.Add("aaat", 1);
                      d.Add("aat", 5);
              

              How can i search in map and get the results back in CCombo. Thanks Raj

              L 1 Reply Last reply
              0
              • R raju_shiva

                Hi sir, Thanks for your reply. I am trying to use as u suggested but getting more confused. Actually i have developed code in VC where i store some values in and store in combo after sorting accordingly. Here is my complete code wt i am doing

                /*During initilizing i am strores vaues in map*/
                CTest1View::OnInitialUpdate()
                {
                CFormView::OnInitialUpdate();
                ResizeParentToFit();

                m\_mapId\["Abc"\];
                m\_mapId\["aa"\];
                

                }
                /*any character is typed in edit box*/
                CTest1View::OnEditchangeCombo1()
                {

                CString strvalue;  	
                CString strTemp;	
                m\_ctrlCombo.GetWindowText(strvalue);	//Get the text from combo
                std::map<CString,CString>::iterator it=m\_mapId.begin();//I will begin using iterator begin
                m\_ctrlCombo.ResetContent();	
                for(; it!=m\_mapId.end();it++)  // I will loop till the map end
                {
                	strTemp = it->first;		//get the first value from map
                	if(strTemp.Find(strvalue)==0)    //find the character in string(which is in map)
                		m\_ctrlCombo.AddString(strTemp); // Add to combo
                
                }
                
                m\_ctrlCombo.SetWindowText(strvalue);	//Get the window text
                    m\_ctrlCombo.ShowDropDown();	//Show the drop down
                

                }

                I am trying with C#

                Dictionary<string, int> d = new Dictionary<string, int>();

                        d.Add("aaat", 1);
                        d.Add("aat", 5);
                

                How can i search in map and get the results back in CCombo. Thanks Raj

                L Offline
                L Offline
                Laxman Auti
                wrote on last edited by
                #8

                raju_shiva wrote:

                How can i search in map and get the results back in CCombo.

                OK. then you could use HashSet with string as a type. This stores unique values and don't need to have key/value pair.

                //Your predefined map values.
                HashSet stringSet = new HashSet { "abc", "aa" };

                //Copy to new array for operations.
                string[] stringList = new string[stringSet.Count];
                stringSet.CopyTo(stringList);

                //Suppose you type "a" in combo box edit.
                string strValue = "a";

                //Look for all the values that contains "a"
                string[] matchedStrings = Array.FindAll(stringList, delegate(string p)
                {
                return (p.Contains(strValue));
                }

                //Sort array for correct sequence in combo box.
                Array.Sort(matchedStrings);

                //You should access predefined combox here.
                //ComboBox cmbValues = new ComboBox();

                //Clear all items from combo box.
                cmbValues.Items.Clear();

                //Fill the combo box with selected values.
                cmbValues.Items.AddRange(matchedStrings);

                //default selection to 0th value.
                cmbValues.SelectedIndex = 0;
                });

                Hope this gives some idea to you.

                Knock out 't' from can't, you can if you think you can. :cool:

                R 1 Reply Last reply
                0
                • L Laxman Auti

                  raju_shiva wrote:

                  How can i search in map and get the results back in CCombo.

                  OK. then you could use HashSet with string as a type. This stores unique values and don't need to have key/value pair.

                  //Your predefined map values.
                  HashSet stringSet = new HashSet { "abc", "aa" };

                  //Copy to new array for operations.
                  string[] stringList = new string[stringSet.Count];
                  stringSet.CopyTo(stringList);

                  //Suppose you type "a" in combo box edit.
                  string strValue = "a";

                  //Look for all the values that contains "a"
                  string[] matchedStrings = Array.FindAll(stringList, delegate(string p)
                  {
                  return (p.Contains(strValue));
                  }

                  //Sort array for correct sequence in combo box.
                  Array.Sort(matchedStrings);

                  //You should access predefined combox here.
                  //ComboBox cmbValues = new ComboBox();

                  //Clear all items from combo box.
                  cmbValues.Items.Clear();

                  //Fill the combo box with selected values.
                  cmbValues.Items.AddRange(matchedStrings);

                  //default selection to 0th value.
                  cmbValues.SelectedIndex = 0;
                  });

                  Hope this gives some idea to you.

                  Knock out 't' from can't, you can if you think you can. :cool:

                  R Offline
                  R Offline
                  raju_shiva
                  wrote on last edited by
                  #9

                  Hi sir, I tried this code but getting an error as HashSet stringSet = new HashSet { "abc", "aa" }; // "A new expression requires () or [] after type" Thanks Raj

                  L 1 Reply Last reply
                  0
                  • R raju_shiva

                    Hi sir, I tried this code but getting an error as HashSet stringSet = new HashSet { "abc", "aa" }; // "A new expression requires () or [] after type" Thanks Raj

                    L Offline
                    L Offline
                    Laxman Auti
                    wrote on last edited by
                    #10

                    Actually written code was correct, but due to incorrect formatting, it was not visible. The code line should be as below.

                    HashSet<string> stringSet = new HashSet<string> { "abc", "aa" };

                    You could check my previous post in FireFox as below. 1. Select the code. 2. Do right click. 3. select "View Selection Source".

                    Knock out 't' from can't, you can if you think you can. :cool:

                    R 1 Reply Last reply
                    0
                    • L Laxman Auti

                      Actually written code was correct, but due to incorrect formatting, it was not visible. The code line should be as below.

                      HashSet<string> stringSet = new HashSet<string> { "abc", "aa" };

                      You could check my previous post in FireFox as below. 1. Select the code. 2. Do right click. 3. select "View Selection Source".

                      Knock out 't' from can't, you can if you think you can. :cool:

                      R Offline
                      R Offline
                      raju_shiva
                      wrote on last edited by
                      #11

                      Hi sir,

                      Laxman Auti wrote:

                      HashSet stringSet = new HashSet { "abc", "aa" };

                      I tried the same ,but stilll the same error Thanks Raj

                      L 1 Reply Last reply
                      0
                      • R raju_shiva

                        Hi sir,

                        Laxman Auti wrote:

                        HashSet stringSet = new HashSet { "abc", "aa" };

                        I tried the same ,but stilll the same error Thanks Raj

                        L Offline
                        L Offline
                        Laxman Auti
                        wrote on last edited by
                        #12

                        raju_shiva wrote:

                        I tried the same ,but stilll the same error

                        Why don't you try something like following?

                        HashSet<string> hashSet = new HashSet<string>();
                        hashSet.Add("abc");
                        hashSet.Add("aa");

                        Knock out 't' from can't, you can if you think you can. :cool:

                        modified on Monday, July 5, 2010 2:45 PM

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups